Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
812 views
in Technique[技术] by (71.8m points)

multithreading - Php: when to use pthread

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...