Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multithreading in PHP

I am trying to create a multi threaded PHP application right now. I have read lots of paper that explains how to create multi threading. All of those examples are built on diving the processes on different worker PHP files. Actualy that is also what I am trying to do but there is a problem :)

There are too many jobs even to divide in 30 seconds (which is the execution time limit)

We are using multi server environment on local network to complete the processes as the processes do not linked to each other or shares the same memory. We just need to fire them up and let them work at an exact time. Each of the processes works for 0.5 secs but it has a possibility to work for 30 secs.

Most of the examples fires up the PHP's and waits for the results. But unfortunately in my situation I dont need to expect a result from the thread. I just need it to execute the command and write the result to its own database.

How can I achieve to fire up the phps and wait for them to work for 10000 processes ?

ADDITIONAL INFO: I know that PHP neither have multi threading feature nor is built for. But we have to create a way to use it for instance we can send request to http://server1/dothis.php?jobid=5 but standart methods makes us wait for the result. If we can manage to send request to this server without waiting for result it would solve our problem I think or we will need completely different approach such as a process divider with c++ or qt.

like image 492
Harun Baris Bulut Avatar asked Feb 24 '10 11:02

Harun Baris Bulut


People also ask

Does PHP support multithreading?

PHP applications, undoubtedly work effectively with multithreading capabilities. Multithreading is something similar to multitasking, but it enables to process multiple jobs at one time, rather than on multiple processes.

How do you do multithreading in PHP?

You can use the pthreads extension in PHP <= 7.4 to enable multithreading in PHP. However, if you are using PHP > 7.4 then it would be great to use the parallel extension. Both of the given extensions can be downloaded from the “PECL” library of extensions.

Is PHP single threaded or multi threaded?

PHP, like most server-side languages, uses multi-threaded, blocking I/O to carry out multiple tasks in parallel.

What are PHP threads?

A thread is a small unit of instructions which can be executed by a processor. A application uses threading if it requires parallelism. In other words by a single program ,we can process multiple unit of instructions parallaly.


2 Answers

As has been pointed out, php doesn't support multi threading. However, and as tomaszsobczak mentioned, there is a library which will let you create "threads" and leave them running, and reconnect to them through other scripts to check their status and so on, called "Gearman".

From the project homepage: "Gearman provides a generic application framework to farm out work to other machines or processes that are better suited to do the work. It allows you to do work in parallel, to load balance processing, and to call functions between languages. It can be used in a variety of applications, from high-availability web sites to the transport of database replication events. In other words, it is the nervous system for how distributed processing communicates."

Rasmus' blog has a great write up about it here: playing with gearman and for your case, it might just be the solution, although I've not read any in depth test cases... Would be interested to know though, so if you end up using this, please report back!

like image 102
dmp Avatar answered Nov 11 '22 23:11

dmp


As the comments say, multi-threading is not possible in PHP. But based on your comment:

If we can manage to send request to this server without waiting for result it would solve our problem I think

You can start a PHP script to run in the background using exec(), redirecting the script's output somewhere else (e.g. /dev/null). I think that's the best you will get. From the manual:

Note: If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

There are several notes and pointers in the User Contributed Comments, for example this snippet that allows background execution on both Windows and Linux platforms.

Of course, that PHP script will not share the state, or any data, of the PHP script you are running. You will have to initialize the background script as if you are making a completely new request.

like image 40
Pekka Avatar answered Nov 11 '22 22:11

Pekka