Last Updated: June 11, 2021
·
53.63K
· nmalcolm

Multi-threading in PHP

Since, and unfortunately, multi-threading is not available in PHP I went on a search for find a solution to create multiple threads of an application. After various queries on Google I found an optimal solution to my problem.

http://blog.motane.lu/2009/01/02/multithreading-in-php/

Working on PHP 5.3, I can easily create as many forks of my application as I need. Simply great for scripts which generate large amounts of data or need to process large amounts of data.

Note that if you're running the script from the command line and need to stop it, ^Z just won't cut it. You'll need to use pkill -9 php to completely stop PHP. This is because forks will keep being created and stopping one process just isn't enough.

As this script uses the pnctl_fork() function, it will only work under Unix systems.

Happy hacking.

8 Responses
Add your response

@euantor Indeed, although for a more stable solution I would suggest taking a look at Zend Server Job Queue ( http://www.zend.com/en/products/server/zend-server-job-queue ). The method above is far from perfect but it gets the job done. A solution is a solution nonetheless. :)

over 1 year ago ·

This one uses Perl for calling a PHP script to consume Tasks from a worker queue in the background.
http://blog.choffmeister.de/?page_id=7
I implemented a few TaskInterface classes to batch/receive large amounts of (slow) work and then process them in the background to send an asynchronous response to the requesting clients later. The initial request is sent an immediate response about the job having been entered in a queue. Later the original requester is sent notification on a callback URL of it's choice.

over 1 year ago ·

Hi,

Unfortunately that's not true - you're not using threads but forks ...

Take a look at :
http://www.geekride.com/fork-forking-vs-threading-thread-linux-kernel/

When you speak about large amounts of shared data you're right, threads could help but forks don't coz the memory is duplicated between the father and the child ...

You can findout this experimental project that's do real threading (but remains limited in comparison to java or c#) :
https://github.com/alecgorge/php_threading

over 1 year ago ·

@ichiriac I am aware of that (Hence the 'fork' tag), but my main reason for using this rather than other solutions (i.e. the threading extension you linked to) is because:

1) It doesn't require any server side changes.
2) CPU/memory isn't an issue in the environment I run it in.

I was referring to a large amount of data as the amount of data of the combined forks handles. 20 forks will perform 20x the work of one worker which is what I required. It's not the most efficient way, but it works, and it works nicely.

As per the original article:

Since threading it’s not available in PHP, I’ve emulated the threads with child processes which are available in php. A thread object simply encapsulates a new process started with pnctl_fork() and emulates – to some extent – the behaviour of the java.lang.Thread class ...

over 1 year ago ·

Please change your title; there are no threads here.

over 1 year ago ·

hi guys, don't worry now. As PHP has been the leading web development language for decades (a staggering 70% contribution to web), its community will not let you down. You can do true threading with pthreads (http://pthreads.org). A multi-threaded web server (http://www.appserver.io) for PHP is in progress using pthreads. You can utilize this Posix threads. Thanks to Joe Watkins for creating pthreads and revitalizing PHP community.

over 1 year ago ·

@euantor Just take a look at my comment below that explains threading in PHP using Pthreads.

over 1 year ago ·

Excellent, it should be useful when writing basic functions and importers

over 1 year ago ·