Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Multithreading with PHP

    How do I create a multithreaded application using PHP. i want to write scripts that run parallelly.

    Please help

  • #2
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #3
    New to the CF scene
    Join Date
    Sep 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Multithreading in PHP

    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);
    ?>

    Hope this will help.

    Cheers!

    Tahir Ata Barry
    CEO, Sulata iSoft

  • #4
    Regular Coder
    Join Date
    Dec 2004
    Location
    Manchester, UK
    Posts
    134
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •