What's the best way to fork/thread in PHP on W

2019-02-15 15:10发布

问题:

I have a php script that checks for updates on many (thousands) of sites. On occasion (more frequently as the number of sites increases), I get an execution timeout on the update of one of these sites, and the whole script goes down the drain.

The best idea I could come up with is to fork each update, so if it dies, the overall update just continues. From what I gathered, it seems PHP threading isn't something to rely on, especially on a windows platform (which I unfortunately have to work on).

I thought about using curl as a forking mechanism, but won't it be much more costly for the server (in time and processing power)?

I'd be happy to hear some ideas and experience about PHP forking/threading...

thanks, Omer.

回答1:

If you're going to use cURL look into the multi* family of methods which allows you to streamline cURL and interacting with a set of sites. That being said, you could also set your max execution time (via ini_set) to some arbitrarily high number and/or keep your own watchdog counter so that you never let it get too high.

But yeah, ultimately are going to run into issues and the lack of full thread support in PHP. So your best bet is to look at other alternatives, e.g. other languages/platforms that provide native threading support.



回答2:

I managed to get some form of threading in php using pcntl extension.It was not the best of solutions but it did the trick.

http://www.php.net/manual/en/ref.pcntl.php

try the following links also, the gave me a idea how to go about the implementation.

http://www.van-steenbeek.net/?q=php_pcntl_fork

http://www.hudzilla.org/phpbook/read.php/16_1_3

http://www.electrictoolbox.com/article/php/process-forking/

I hope this helps , but php is not very good with threading though.



回答3:

You could set the ini directive max_execution_time to 0. This should remove the max execution time, and allow the script to run without incurring this error. This value has to be set in your php.ini file however - using ini_set doesn't work.



回答4:

set_time_limit(0);

set_time_limit docs



回答5:

Well, at the end I went for curl, and it works just fine.

I needed a cross-platform solution, as I develop on a Mac while in this case the production serer is Windows. That meant pcntl was out of the question.

I was worried that sending thousands of curl requests to my own server might hog it and bother users on the site at the time, but I was mistaken.

I did, however, have to add a set_time_limit(0) to the script that initiates all the curl calls, otherwise it just times out.