views:

181

answers:

4

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?

A: 

Lothar,

As far as I know, php don't work with services, like his concorrent, so you don't have a way for php to know how much time have passed unless you're constantly interrupting the process to check the time passed .. So, imo, no, you can't do that in php :)

yoda
I don't understand your answer. But i'm pretty sure that PHP is counting only real time for there script termination calculation and not wall time.
Lothar
+5  A: 

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.
Spencer Ruport
+2  A: 

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.

Brent Baisley
It's not about me. I'm writing a canned solution that tousands of customers might run on hunderts on cheapest hosting providers. I simply can't assume that they have the right to do anything else then runnig php on the machine. And many have a very time small time limit. I've seen 15 seconds for example. For a data processing background task this are only problems.
Lothar
I've used this type of solution on a $10/month shared server and on a quad core dedicated server. I just parameterize the number of processes that will be spawned to scale it up or down.
Brent Baisley
+1  A: 

Out of curiosity - what is your "large algorithmic problem" attempting to accomplish?

You might be better to write it as an Amazon EC2 service, then sell access to the service rather than the package itself.

Edit: you now mention "mass emails". There are already services that do this, they're generally known as "spammers". Please don't.

Hugh Bothwell
Well even small companies come up with 10000 double opt in customers for there newsletter. It's not that hard.
Lothar