PHP:何时使用pthread [英] Php: when to use pthread

查看:45
本文介绍了PHP:何时使用pthread的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用线程了解不多,但是我研究了pthreads for php,它看起来非常有趣,容易,甚至比我想象的要容易...

我搜索了示例并浏览了文档,但是我找不到任何实际示例来说明何时使用线程实际上是有益的,它肯定是用于长任务的,而这些任务并不相互依赖,就像做很多事情一样. http请求或发送邮件.

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

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

任何建议或示例都将不胜感激!

解决方案

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

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

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

pthread创建的线程既稳健又安全,就像Zend自己通过多线程SAPI设置为服务请求的线程一样.它们与所有相同的功能兼容,再加上您期望从高级线程API获得的所有功能(几乎).

在无共享体系结构的底层实现深层线程总是存在局限性,但是就更好地使用可支配的物理资源而言,其好处在于,PHP对任何给定任务的整体可用性远远超过了其价值.在那种环境下工作的开销.

pthreads中包含的对象的工作方式与任何其他PHP对象一样,您可以在任何引用对象的上下文中读取,写入和执行其方法.

您正在按照正确的思路进行思考:效率的衡量标准并不在于应用程序执行的线程数,而是在于如何利用这些线程来最佳地满足应用程序的主要目的.工人是一个好主意,只要您可以在任何地方使用它们,就可以这样做.

关于您所询问的特定问题,LoggingWorker是一个好主意,可以正常工作,不要试图共享该流,因为没有意义,如果Worker打开日志文件,它将是非常稳定的;或者数据库连接和由它执行的可堆栈可访问它们.再次在示例中包含一个SQLWorker,这是API缺乏体面的异步API的另一个好主意,或者您只是更喜欢多线程编程的流程.

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

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...

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.

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!

解决方案

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

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.

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).

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.

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.

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.

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

这篇关于PHP:何时使用pthread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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