简体   繁体   中英

Multiupload using pthread in php

I have been trying to implement multi-threading in php to achieve multi-upload using pthreads php .

From my understanding of multi-threading , this is how I envisioned it working.

I would upload a file,the file will start uploading in the background; even if the file is not completed to upload, another instance( thread ) will be created to upload another file. I would make multiple upload requests using AJAX and multiple files would start uploading, I would get the response of a single request individually and I can update the status of upload likewise in my site.

But this is not how it is working. This is the code that I got from one of the pthread question on SO, but I do not have the link( sorry!! ). I tested this code to see of this really worked like I envisioned. This is the code I tested, I changed it a little.

<?php

error_reporting(E_ALL);

class AsyncWebRequest extends Thread {
    public $url;
    public $data;

    public function __construct ($url) {
        $this->url = $url;
    }

    public function run () {
        if ( ($url = $this->url) ){
            /*
            * If a large amount of data is being requested, you might want to
            * fsockopen and read using usleep in between reads
            */
            $this->data = file_get_contents ($url);
            echo $this->getThreadId ();
        } else{
            printf ("Thread #%lu was not provided a URL\n", $this->getThreadId ());
        }
    }
}

$t = microtime (true);


foreach( ["http://www.google.com/?q=". rand () * 10, 'http://localhost', 'https://facebook.com'] as $url ){
    $g = new AsyncWebRequest( $url );
    /* starting synchronized */
    if ( $g->start () ){
        printf ( $url ." took %f seconds to start ", microtime (true) - $t);
        while ($g->isRunning ()) {
            echo ".";
            usleep (100);
        }
        if ( $g->join () ){
            printf (" and %f seconds to finish receiving %d bytes\n", microtime (true) - $t, strlen ($g->data));
        } else{
            printf (" and %f seconds to finish, request failed\n", microtime (true) - $t);
        }
    }
    echo "<hr/>";
    }

So what I expected from this code was it would hit google.com , localhost and facebook.com simultaneously and run their individual threads. But every request is waiting for another request to complete.

这是我得到的回应

For this it is clearly waiting for first response to complete before it is making another request because time the request are sent are after the request from the previous request is complete.

So, This is clearly not the way to achieve what I am trying to achieve. How do I do this?

You might want to look at multi curl for such multiple external requests. Pthreads is more about internal processes.

Just for further reference, you are starting threads 1 by 1 and waiting for them to finish. This code: while ($g->isRunning ()) doesn't stop until the thread is finished. It's like having a while (true) in a for. The for executes 1 step at a time.
You need to start the threads, add them in an array, and in another while loop check each of the threads if it stopped and remove them from the array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM