Page 1 of 2

Multi-threading in PHP

Posted: Wed Aug 13, 2003 6:54 pm
by Seth_[php.pl]
I wonder if anyone of you have writen a multi-thread script (working ;)) which could be used on any system ?

I was trying to do that with external program such as C++ application which was executing scripts in threads... but with no result :P

Alsow cooperate with java functionality... but there was some errors too.

Now I'm experiments with register_tick_function.


Some tips ?

Posted: Wed Aug 13, 2003 7:00 pm
by qartis
Ticks, declare and heartbeats (in php) are NOT multitasking/multithreading. They are (as stated in user comments on php.net) just an overly-complex way of calling a set of functions on demand.

For the life of me, I couldn't find one source for info on php multithreading, so as far as I'm concerned, it doesn't exist. You may need to investigate using 2 php scripts simultaneously.

Posted: Sun Aug 17, 2003 4:14 am
by BDKR
It would be difficult, but you could mimic multi-threading by forking child procs and keeping communication with them using shared memory or system messaging. I've wanted to screw with this but haven't had the time.

Cheers,
BDKR

Multithreading in PHP

Posted: Sat Sep 02, 2006 3:17 am
by tabarry
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


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.

Code: Select all

<?
//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.
for($i=1;$i<=10;$i++){
	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.

Code: Select all

<?
//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());
while($row=mysql_fetch_array($rs)){
   //send mail here to each record
}
mysql_free_result($rs);
?>
Hope this will help.

Cheers!

Tahir Ata Barry
CEO, Sulata iSoft


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Re: Multithreading in PHP

Posted: Sat Sep 02, 2006 7:36 am
by feyd
tabarry wrote: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.
technical faux pas: you've described multiprocessing, not multithreading; and you are using the web server to create these processes, not php itself.

Posted: Sat Sep 02, 2006 11:05 am
by BDKR
It's Golden Hammer action at it's best when you get right down to it.

Good and oft used methods would be to either spin threads, which of course PHP can't do, or fork child procs, which PHP can do. PHP can even share memory and pass messages so that all child procs can be kept up to date as to the on goings.

If any of that sounds familiar BTW, Apache 1.X is based on something very similar. Child processes that all share the same region of memory that's called the "Scoreboard".

That said, it's not at all a far fetched concept.

Cheers,
BDKR

Posted: Sat Sep 02, 2006 11:51 am
by Ollie Saunders
At the risk of sounding very n00bish indeed.

What use is multithreading?

Posted: Sat Sep 02, 2006 12:24 pm
by Ambush Commander

Posted: Sat Sep 02, 2006 12:40 pm
by Ollie Saunders
I know what threads are but way do you need them in a web application, apache multi-threads the requests anyway doesn't it?

Posted: Sat Sep 02, 2006 12:41 pm
by Ambush Commander
When a web-application isn't doing traditional web-application work, for instance, like sending a thousand emails.

Posted: Sat Sep 02, 2006 1:02 pm
by BDKR
ole wrote:I know what threads are but way do you need them in a web application, apache multi-threads the requests anyway doesn't it?
Which version of Apache are we talking about? Apache 1.x uses a forked process model. Apache 2 is capable of true multi-threading.

Posted: Sat Sep 02, 2006 1:10 pm
by Ollie Saunders
Which version of Apache are we talking about?
Your telling me.
When a web-application isn't doing traditional web-application work, for instance, like sending a thousand emails.
OK cool. Gimme more examples! In the wider sense of software development, when is multi-threading used? (I know about Operating Systems).

Posted: Sat Sep 02, 2006 1:19 pm
by BDKR
ole wrote: Gimme more examples!
Imagine a transaction server that maintains connections to hundreds of clients simultaneously. Any of those can request something at any time. None of those requests are going to be the same, and therefore, some may take longer then others. With threading, those requests are handled in one process without effecting processes around it. In other words, no one request is "blocking". Or to put it in other words, the server isn't doing nothing while the this one single request tries to finish. Instead, other threads will be given processor time as well so the server isn't stalling every incomming request while it tries to finish one.

Hope that helps.

Posted: Sat Sep 02, 2006 1:25 pm
by Ollie Saunders
Thanks BDKR that does help.
Makes me think, why isn't MySQL multi-threaded?

Posted: Sat Sep 02, 2006 1:35 pm
by BDKR
ole wrote:Thanks BDKR that does help.
Makes me think, why isn't MySQL multi-threaded?
It is multi-threaded.