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.
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.
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.
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.
<?
//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.
<?
//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);
?>
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]
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.
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".
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.