jtex1a
Last Updated: March 07, 2016
·
20.87K
· nmalcolm
 body onload alert 1

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

9
Irnhmv6

Great find Nathan! Should be useful when I'm writing basic importers and functions to hammer through thousands of pieces of data.

over 1 year ago ·
14
 body onload alert 1

@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 ·
1180
Aaac8928940ca0d0b574e1bad271a46b

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 ·
1651
2017 cs

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 ·
1663
 body onload alert 1

@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 ·
6461
755ad21d2059ac3970754edd621ba65b

Please change your title; there are no threads here.

over 1 year ago ·
12952
Default profile 4 normal

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 ·
12955
Default profile 4 normal

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

over 1 year ago ·
Awesome Job

00bd5e74 0068 11e7 919e d8a256dfa511
DevOps Engineer
·
San Francisco, CA or Washington, DC
·
Full Time