Multi-threading in PHP

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
Seth_[php.pl]
Forum Commoner
Posts: 30
Joined: Sun Aug 10, 2003 5:25 am
Location: Warsaw / Poland

Multi-threading in PHP

Post 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 ?
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post 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.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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
tabarry
Forum Newbie
Posts: 1
Joined: Sat Sep 02, 2006 2:34 am

Multithreading in PHP

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Re: Multithreading in PHP

Post 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.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

At the risk of sounding very n00bish indeed.

What use is multithreading?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

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

Post by Ambush Commander »

When a web-application isn't doing traditional web-application work, for instance, like sending a thousand emails.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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).
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Thanks BDKR that does help.
Makes me think, why isn't MySQL multi-threaded?
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

ole wrote:Thanks BDKR that does help.
Makes me think, why isn't MySQL multi-threaded?
It is multi-threaded.
Post Reply