Php: when to use pthread
Asked Answered
J

1

32

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!

Judithjuditha answered 2/1, 2013 at 17:49 Comment(6)
php is not multithreaded, and most likely never will be. many of its internal libraries and add-ons are not thread-safe. you can "fake" threads by spawning copies of php via fork, exec, popen, etc.. but they're not true threads, and the overhead vastly outweighs any microscopic gains you might hope to get.Convulsant
Php is not multi threaded, but looks like there is a pecl extension that gives php multi thread capabilities, github.com/krakjoe/pthreads seems legitJudithjuditha
Be carefull when you use threads for database stuff: As every thread creates it´s own MySql-Resource, you will suddenly find yourself outside of transactions.Airflow
PHP's internal framework has been threadsafe for over a decade. It took time for extensions to adapt, but all uptodate extensions are threadsafe as well. It doesn't do any good to parrot old reviews and out dated fanboy flam.Wernsman
If I'm not mistaken, extensions must be threadsafe to be officially supported by PHP. It's been a long while since TSRM was finalized and fully integrated into PHP. To be specific, all extensions included in the threadsafe windows distribution are threadsafe at their core. Yes, this is all major extensions.Wernsman
did you get answer to "can I return data from a thread?"Venery
U
108

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.

Unroll answered 3/1, 2013 at 19:41 Comment(5)
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...Unroll
@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 oneCroner
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: https://mcmap.net/q/75245/-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.Unroll
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 ....Croner
@joe Thanks for the wonderful extension. Perhaps the examples can go into official php.net documentation ?Adabelle

© 2022 - 2024 — McMap. All rights reserved.