用于 PHP 无限循环的 Pthreads 与 Parallel [英] Pthreads vs Parallel for infinite loops on PHP

查看:83
本文介绍了用于 PHP 无限循环的 Pthreads 与 Parallel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在 PHP 上执行多线程的方法,并遇到了 pthreads PHP API,我认为它很容易实现(但是我必须找出如何安装支持 ZTS 的 PHP 版本对于 Debian).

I was looking for a way to perform multi-threads on PHP and came across the pthreads PHP API, which I think could be easy to implement (however I'll have to find out how to install the PHP version with ZTS support for Debian).

问题是,当我查看 pthreads php.net 文档时,我发现了这个:

The thing is that when I was looking at the pthreads php.net documentation I found this:

提示考虑改用并行.

我不知道的.

我的目标是获得一个项目列表,并为每个项目打开一个 websocket,它永远监听某些更新.所以线程应该永远存在,如果被杀死或停止,它应该重新启动(但我认为我可以在外部处理这个).

My goal is to get a list of items and for each one, open a websocket that listens for certain updates forever. So the thread should be there forever and if killed, or stopped, or so, it should be restarted (however I think I can handle this externally).

我不确定哪种情况最适合这种情况.有什么推荐吗?

I'm not sure which one would be the most appropriate for this situation. Any recommendation?

推荐答案

首先要说的是,您的应用程序的描述中没有任何需要使用线程的内容,看起来您真正想要的是异步并发:异步并发通常用于通过消除不必要的等待来增加 I/O 绑定代码的吞吐量.并行并发通常用于通过利用更多内核来提高 CPU 绑定代码的吞吐量.

The first thing to say is that there is nothing in the description of your application that demands the use of threads, it would seem what you really want is asynchronous concurrency: Asynchronous concurrency is typically used to increase the throughput of I/O bound code, by eliminating unnecessary waiting. Parallel concurrency is typically used to increase the throughput of CPU bound code, by utilising more cores.

有一些很好的框架可以在 PHP 中实现非阻塞 I/O,我推荐的是 amphp.

There are a few good frameworks for implementing non-blocking I/O in PHP, the one I would recommend is amphp.

当然,我可能没有对您的应用程序的完整描述,所以转到 pthreads 的主题:它被parallel过时了,新项目应该使用parallel.

Of course, I may not have a full description of your application, so onto the subject of pthreads: It is made obsolete by parallel, new projects should use parallel.

免责声明:我写了 pthreads 和 parallel ...

这篇关于用于 PHP 无限循环的 Pthreads 与 Parallel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆