View Full Version : Multithreading with PHP
09-09-2005, 09:25 PM
How do I create a multithreaded application using PHP. i want to write scripts that run parallelly.
09-10-2005, 12:13 AM
It won't work on webservers and windows. The process control extention is ment for non webserver PHP applications. http://www.php.net/manual/en/ref.pcntl.php
09-02-2006, 10:22 AM
Yes, you can do multithreading in PHP. As there is no way to do it directly, here is an example for you to understand.
Let us suppose you want to send 1000 emails, using 10 threads and each thread will send 100 emails, so here is how you do it.
You will need two pages. One page to call the task and the other page will be the task itself.
Make two pages. One as main.php and the other as thread.php.
thread.php page will contain your code to send emails from people in the database and main.php will build html image <img> tag in loop. This image tag will not have an image in it, but instead will call thread.php file in a remote instance.
The code for main.php page is something like this.
//START A LOOP
//thread.php is being sent with 3 parameters in query string.
//'from' parameter will tell the thread.php page that where the records should start.
//'to' parameter will tell the thread.php page that where the records should end.
//change the above 2 parameters to whatever you want
//'refresh' parameter is being passed with the $i variable to make sure the page is not cached.
echo "<img border=0 src='thread.php?from=1&to=1000&refresh=$i' width=0 weight=0>";
The code for thread.php page is something like this.
//connect to database here....
$from = $_GET['from'];
$to = $_GET['to'];
$sql="SELECT * FROM mailing_list_table WHERE list_id between $from and $to";
$rs = mysql_query($sql) or die(mysql_error());
//send mail here to each record
Hope this will help.
Tahir Ata Barry
CEO, Sulata iSoft
09-02-2006, 03:45 PM
No no no no no. OMG, that's terrible.
For one, it's not multithreading, multithreading uses 1 process, for two, why use separate windows when PHP can spawn its own new processes? Ouch.
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.