Categories
PHP

Multithreading in PHP: Doing it right!

Long ago I wrote a similar post on multi threading in php – http://www.masnun.com/2009/09/07/php-multi-threading.html – which allowed us to fork a new process and execute codes in parallel. While it often served the purpose right, it was not true multi-threading.

I have come across the awesome “pthreads” PECL extension for some time. Tonight, I planned to give it a shot. What sparked my curiosity? Well, let me quote them –

this is not a hack, we don’t use forking or any other such nonsense, what you create are honest to goodness posix threads that are completely compatible with PHP and safe … this is true multi-threading 🙂

True multi-threading in PHP, isn’t that worth wasting a night’s sleep to check out?

Installation

Since it’s a PECL extension, you can just do –

It requires ZTS aka thread safety enabled in PHP. So if your PHP is non-zts, please make sure to recompile with zts. Please consult appropriate docs on your platform on how to do that.

If you’re on OS X and use homebrew php, you can use the “–with-thread-safety” flag to reinstall php with ZTS enabled. Now install the extension and it should work.

Let’s write some codes

Of course, here we go!

The code is pretty straightforward. We extend the Thread class which has an abstract method – “run” which we must implement with our business logic. The codes inside this method actually get executed when the threads run. Then we create 5 instances and call the “start” method. After the threads have started, we again call the “join” method on each. This will ask our main thread to wait for the child threads to return.

As you can see, the main worker threads all output their ID, thus we can identify which one executed when. This could be helpful to determine if they were actually running in threads (and thus being async in nature) or ran one after one.

I got these outputs from several run:

Okay, so looks like it worked. No?

Here’s some helpful links:

Github Repo: https://github.com/krakjoe/pthreads

Manual: http://docs.php.net/manual/en/book.pthreads.php

20 replies on “Multithreading in PHP: Doing it right!”

Don’t know if you are having troubles with it.. but have you tried Pooling? I can’t find any material over internet anywhere where I could find some sample lengthy code for pooling. Strange thing is, during the normal thread procedure my threads consume about 600MB of memory and cause a lot of laod but run succesfully, but when I start pooling my tasks even a dummy script after a long list of tasks goes to segmentation fault. Did you ever try with pooling and if yes can you help me with too? Thanx in advance!

I came across this comment during my many hours trying to introduce myself to the topic of parallel programming in php. Still just trying to figure out the difference between pthreads, pcntl_fork, exec, queues, etc. Can you answer a couple of questions about your last comment for me?

By multiple workers do you mean increasing the numproc config value?

are these multiple workers just forks? or POSIX?

Is there a limit to how many workers you can use?

Do you know of a good resource to help me get a better understanding of all this?

Thanks!

Good post. By the way the mentioned project appserver.io which is a fully featured application server entirely written in PHP is now available in a stable version 1.0.0. Maybe you will find some time checking it out and write a review.

Thanks

Great example to see how it works, however, I seem to have a misconception? Since the threads start and then run independently (asynchronously), the main program should be able to continue. I added a short echo to test this, but the result shows my text after the workers are closed.
Adjusted code:

Actual result:

Expected result:

Comments are closed.