PDA

View Full Version : Parallel Processing in PHP


alirizwan
08-09-2005, 23:36/11:36PM
Hi,

I want to perform Parallel Processing in PHP. Let me try to explain my problem.
I want to open 2 files with fopen at the same time.

For Example first file takes 30 seconds and 30 seconds seond file to read in web.
and total time will be 60 seconds.
Can i read both files in 30 seconds with parallel execuation?

<?php

$handle_1 = fopen("www.domain.com/index.php", "r");
$text_1 = fread($handle_1,filesize("$handle_1"));
fclose($handle_1);

$handle_2 = fopen("www.domain.com/index.php", "r");
$text_2 = fread($handle_2,filesize("$handle_2"));
fclose($handle_2);

$text = $text_1 . $text_2;

print $text;

?>

As above code run line by line and takes much time. Is there any way that i can open both or more files Simultaneous ?
How can i execute some my code in parallel processing?


Regards,

Ali Rizwan
[URL removed - SIG files not allowed!]

bigDugan
08-09-2005, 23:50/11:50PM
And what does your tag line and website address have to do with your question?

Connie
09-09-2005, 00:06/12:06AM
Hi Ali and welcome maybe. :hi:
Please read the Forum Guidelines (http://www.ihelpyouservices.com/forums/showthread.php?s=&threadid=18677) . If you want to be a good member you are welcome. If not don't bother us.

WebSavvy
09-09-2005, 00:10/12:10AM
alirizwan, I've removed the appending SIG line from your post. Please note that SIG files are turned off on these forums.

You have the ability to add your site URL to your profile via your User CP. This link then becomes available via the hyperlinked www button located directly below all replies made by you, should anyone be interested enough to visit your site.

If you are unclear about what is acceptable here, then please reread the Forum Guidelines (http://www.ihelpyouservices.com/forums/showthread.php?s=&threadid=18677) which were made available to you during the registration process in order to become a member of these forums.

Thanks.

ArmenT
09-09-2005, 13:29/01:29PM
PHP doesn't really support threading, but there are a couple of ways to handle this:

Method 1
Write a perl script to fetch pages and write them to a file somewhere (You can also write the page-fetching script in C, python, perl etc. or use wget or lynx to do this). From your PHP script, call your perl/wget/lynx using the system() call and use & at the end of the command line. The & tells the shell to run the program in the background and allows your PHP script to continue to the next statement. Assuming you're using wget:

system("wget http://www.domain1.com/file.html &");
system("wget http://www.domain2.com/file2.html &");
while (!file_exists("file1.html") && !file_exists("file2.html"))
; // Twiddle thumbs while it retrieves both files
// Now do something here with both files.


Method 2
This only works with PHP 5. Instead of using fopen(), use stream_socket_open() to open native sockets to both files. Then you can use stream_select() to see which socket needs attention (i.e. has data to read or needs data to be written etc.) and use fread() or fwrite() to read/write data. Of course, ths means you need to know how to write your own HTTP request (it's not that hard to learn, if you don't already know how). This solution is completely written in PHP, but you need some technical knowledge and PHP 5 for it to work.