Php:何时使用 pthread

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14126696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:42:15  来源:igfitidea点击:

Php: when to use pthread

phpmultithreadingpthreadsdie

提问by joschua011

I don't know much about using threads but I looked into pthreads for php and it seems very interesting and easy, or easier than I thought...

我对使用线程不太了解,但我查看了 php 的 pthreads,它看起来非常有趣和简单,或者比我想象的更容易......

I searched for examples and looked through the documentation but I couldn't find any real-world examples of when it is actually beneficial to use threads, it sure is for long tasks that don't depend on each other like doing many http requests or maybe sending mails.

我搜索了示例并查看了文档,但我找不到任何实际使用线程实际上有益的示例,它确实适用于不依赖彼此的长任务,例如执行许多 http 请求或也许发送邮件。

But what about Writing log entries? Inserts to databases? (like tracking user activity) Fetching from database (can I return data from a thread?)

但是写日志条目呢?插入数据库?(例如跟踪用户活动)从数据库中获取(我可以从线程返回数据吗?)

Will this increase performance or is the overhead of creating threads too much? (although I could use a worker pool too get less overhead, I think... )

这会提高性能还是创建线程的开销太大?(虽然我也可以使用工作池来减少开销,我认为......)

Any advice or examples are greatly appreciated!

非常感谢任何建议或示例!

回答by Joe Watkins

There are many examples included in the distribution and available on github:

发行版中包含许多示例,可在 github 上找到:

https://github.com/krakjoe/pthreads/tree/master/examples

https://github.com/krakjoe/pthreads/tree/master/examples

These examples include such things as a general purpose thread pool, a multi-threaded socket server and an SQLWorker.

这些示例包括诸如通用线程池、多线程套接字服务器和 SQLWorker 之类的东西。

The Threads pthreads creates are as sane, and as safe, as the threads that Zend itself sets up to service requests via a multi-threaded SAPI. They are compatible with all the same functionality, plus all you expect from a high level threading API (nearly).

pthreads 创建的线程与 Zend 本身通过多线程 SAPI 为服务请求设置的线程一样理智和安全。它们兼容所有相同的功能,以及您对高级线程 API(几乎)的所有期望。

There will always be limitations to implementing threading deep in the bowels of a shared nothing architecture, but the benefits, in terms of using better the physical resources at your disposal, but also the overall usability of PHP for any given task far outweigh the overhead of working around that environment.

在无共享架构的内部深处实现线程总是有限制的,但是在更好地使用物理资源方面的好处,以及 PHP 对任何给定任务的整体可用性远远超过在该环境中工作。

The objects included in pthreads work as any other PHP object does, you can read, write and execute their methods, from any context with a reference to the object.

pthreads 中包含的对象与任何其他 PHP 对象一样工作,您可以从任何上下文中读取、写入和执行它们的方法并引用该对象。

You are thinking exactly along the right lines: a measure of efficiency is not in the number of threads your application executes but how those threads are utilized to best serve the primary purpose of the application. Workers are a good idea, wherever you can use them, do so.

您完全按照正确的思路思考:衡量效率的不是您的应用程序执行的线程数,而是如何利用这些线程来最好地服务于应用程序的主要目的。工人是个好主意,只要你能使用他们,就去做。

With regard to the specific things you asked about, a LoggingWorker is a good idea and will work, do not attempt to share that stream as there is no point, it will be perfectly stable if the Worker opens the log file, or database connection and stackables executed by it can access them. An SQLWorker is included in the examples, again, another good idea where the API lacks a decent async API, or you just get to prefer the flow of multi-threaded programming.

关于您询问的具体问题,LoggingWorker 是一个好主意并且会起作用,不要尝试共享该流,因为没有意义,如果 Worker 打开日志文件或数据库连接并且由它执行的可堆叠可以访问它们。SQLWorker 包含在示例中,这又是另一个好主意,其中 API 缺乏像样的异步 API,或者您只是更喜欢多线程编程的流程。

You won't get a better, or more correct answer: I wrote pthreads, on my own.

您不会得到更好或更正确的答案:我自己编写了 pthreads。