Page 1 of 1

multi threading in PHP

Posted: Fri Nov 18, 2005 11:50 am
by jasongr
Hello

I have a PHP application which needs to invoke several URLs using file_get_contents
each URL will return its own set of values

Is there a way to spawn multiple threads in PHP, letting each one handle its own URL with file_get_content?
Otherwise, doing it sequencially will take a long time, where I have to wait for one call of file_get_content to finish before the next can begin

if this is possible, I would apprciate a link on the subject for reading

Posted: Fri Nov 18, 2005 12:17 pm
by yum-jelly
Linux or Windows?

yj

Posted: Fri Nov 18, 2005 1:22 pm
by John Cartwright
PHP is single threaded

Posted: Fri Nov 18, 2005 4:40 pm
by Ambush Commander
Via the pcntl extension (unix only, although I have seen Windows CGYWIN ports), PHP supports forking and other fun stuff.

Posted: Fri Nov 18, 2005 4:51 pm
by onion2k
I can think of two solutions .. which to use depends on which version of PHP you're using ..

1. http://uk.php.net/manual/en/function.cu ... i-exec.php .. CURL-Multi can get lots of pages at once. PHP5 only though. And there's practically no documentation.

2. Use fsockopen() to open some sockets to the servers you're getting data from, set them all to non-blocking with stream_set_timeout(), and write the HTTP requests yourself.

Posted: Fri Nov 18, 2005 4:54 pm
by Ambush Commander
Actually, I would be interested in a Windows solution to this problem. I came across pcntl while I was trying to do the exact same thing, but the CYGWIN build proved too limiting.

Posted: Fri Nov 18, 2005 5:30 pm
by timvw
php5 has curl_multi_* functions :)

Here is an example of how to use it: http://blog.bitflux.ch/archive/2005/10/ ... feeds.html.


(On php4 i simply have a cronjob that fetches the complete load every 30mins and stores the fetched stuff in a database).