multi threading in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

multi threading in PHP

Post 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
yum-jelly
Forum Commoner
Posts: 98
Joined: Sat Oct 29, 2005 9:16 pm

Post by yum-jelly »

Linux or Windows?

yj
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

PHP is single threaded
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Via the pcntl extension (unix only, although I have seen Windows CGYWIN ports), PHP supports forking and other fun stuff.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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).
Post Reply