Php: when to use pthread

27,209

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.

Share:
27,209
joschua011
Author by

joschua011

Updated on July 18, 2022

Comments

  • joschua011
    joschua011 almost 2 years

    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!

  • Joe Watkins
    Joe Watkins over 11 years
    Thanks :) I wasn't being a clown, pthreads is up against an unbelievable amount of content on the internet, that is frankly, wrong. If any of the things people said about PHP and it's thread safety or reliability were true, pthreads simply would not work. When faced with facts we do not understand the thing to do is research, not repeat everything you once read about a subject and wrap it up like an answer. I was disappointed that someone with such a vast amount of points under their belt didn't deem it necessary to look further than the past...
  • Baba
    Baba over 11 years
    @JoeWatkins i was referring to the person that down voted your answer (Who the heck downvoted this? – Charles and i replied .... The person must be a clown) .... Well done Joe Thanks for the pthreads and for adding value to PHP .... Am expecting to hear when this would be part of the cored PHP .. Nice one
  • Joe Watkins
    Joe Watkins over 11 years
    Oh, I felt cold for having downvoted someone who puts so much effort into helping, I do feel a bit of a clown about that, but hopefully it's understanable ... I explain the past, design, implementation and implications in great detail in the following answer: stackoverflow.com/questions/209774/does-php-have-threading/… The fact is, it may never be included in the core ( nor is apc, lets not forget to be integral doesn't require inclusion in the core ), for a rationale of that see the post I have linked too. Thanks for your kind words.
  • Baba
    Baba about 11 years
    I really think you should update the PHP documentation with some real live examples and use cases .. this way more people can get to use and from the feedback the lib get better ... Don't worry i let people know about this ....
  • Ashkan Kh. Nazary
    Ashkan Kh. Nazary about 10 years
    @joe Thanks for the wonderful extension. Perhaps the examples can go into official php.net documentation ?