mardi 27 septembre 2016

PHP: Not able to start Thread from Laravel Command

I am new to pthreads in php. I have learnt that it is not possible to start it from apache through a webserver so I have resorted into using laravel commands to run it from the command line but I get this error when I call the start() method of the thread

PHP Fatal error:  Uncaught Exception: Serialization of 'Closure' is not allowed in [no active file]:0

Stack trace:

#0 {main}

thrown in [no active file] on line 0

but the error goes away whenever I call the run method, which is not the way to go.

I will really appreciate any help I can get.

the handle method of my command goes thus

public function handle()
{
    $threads = ["Thread One", "Thread Two"];
    $threadList=[];
    foreach ($threads as &$thread){
        //$id++;
        $this->info($thread);
        $testThread = new TestThread($thread);
        $testThread->start();
        $threadList[] = $testThread;
    }

    foreach ($threadList as $thread){
        $this->info("waiting for thread ". $thread->getThreadID());
        $thread->join();
        $this->info($thread->getThreadID()."done ");
        //echo $thread->getThreadID().'<br/>';
    }
}

and my ThreadClass is this

namespace App\Engine\Threads\ThreadClass;


use Illuminate\Support\Facades\Log;

class TestThread extends \Thread
{
    private $threadID;

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

    /**
     * @return mixed
    */
    public function getThreadID()
    {
       return $this->threadID;
    }

    public function run()
    {
       $this->threadID = $this->threadID.''.mt_rand(4000, 5000);

    }


 }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire