Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multithreaded Programming in PHP to avoid runtime limitations

I know about PHP not being multithreaded but i talked with a friend about this: If i have a large algorithmic problem i want to solve with PHP isn't the solution to simply using the "curl_multi_xxx" interface and start n HTTP requests on the same server. This is what i would call PHP style multithreading.

Are there any problems with this in the typical webserver environment? The master request which is waiting for "curl_multi_exec" shouldn't count any time against its maximum runtime or memory length.

I have never seen this anywhere promoted as a solution to prevent a script killed by too restrictive admin settings for PHP.

If i add this as a feature into a popular PHP system will there be server admins hiring a russian mafia hitman to get revenge for this hack?

like image 663
Lothar Avatar asked Jul 02 '26 09:07

Lothar


2 Answers

If i add this as a feature into a popular PHP system will there be server admins hiring a russian mafia hitman to get revenge for this hack?

No but it's still a terrible idea for no other reason than PHP is supposed to render web pages. Not run big algorithms. I see people trying to do this in ASP.Net all the time. There are two proper solutions.

  1. Have your PHP script spawn a process that runs independently of the web server and updates a common data store (probably a database) with information about the progress of the task that your PHP scripts can access.
  2. Have a constantly running daemon that checks for jobs in a common data store that the PHP scripts can issue jobs to and view the progress on currently running jobs.
like image 74
Spencer Ruport Avatar answered Jul 04 '26 22:07

Spencer Ruport


By using curl, you are adding a network timeout dependency into the mix. Ideally you would run everything from the command line to avoid timeout issues.

PHP does support forking (pcntl_fork). You can fork some processes and then monitor them with something like pcntl_waitpid. You end up with one "parent" process to monitor the children it spanned. Keep in mind that while one process can startup, load everything, then fork, you can't share things like database connections. So each forked process should establish it's own. I've used forking for up 50 processes.

If forking isn't available for your install of PHP, you can spawn a process as Spencer mentioned. Just make sure you spawn the process in such a way that it doesn't stop processing of your main script. You also want to get the process ID so you can monitor the spawned processes.

exec("nohup /path/to/php.script > /dev/null 2>&1 & echo $!", $output);
$pid = $output[0];

You can also use the above exec() setup to spawn a process started from a web page and get control back immediately.

like image 30
Brent Baisley Avatar answered Jul 04 '26 22:07

Brent Baisley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!