0

I am using Laravel 5.1 and I am trying to start several threads within a Command class:

$documents->chunk(1000, function($documentChunk) use (&$threads, &$threadNumber, &$repository) {
    $threads[$threadNumber] = new MyThread($documentChunk);
    $threads[$threadNumber]->start();
    $this->info("Thread [".$threadNumber."] running");
    $threadNumber++;
});

The $documents variable has been previously filled with some data retrieved from the database

In the MyThread class this is what I have:

<?php namespace App\Helpers;

use Download;

class MyThreadextends \Thread {

    protected $chunk;

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

    public function run() {
        ...
        $downloads = Download::select(...);
        ...
    }

}

On the select line, I get a:

PHP Fatal error: Class 'Download' not found

However, if I call directly $threads[$threadNumber]->run() instead of $threads[$threadNumber]->start(), I do not have this error. Maybe it is a problem of class loading but I can't figure out what's the real problem and if it can be solved...

If someone could help me with this, I would be really grateful since it's been hours I'm on this problem.

Thanks, Killian.

2 Answers2

0

You need to setup your autoloader, help here

I think the example is Symfony, but the same thing applies.

Joe Watkins
  • 17,032
  • 5
  • 41
  • 62
0

I've finally found a workaround. I don't use threads anymore but Guzzle. The code located in my thread is now in a controller method, accessible via an URL - I made a REST API. Guzzle allows me to do asynchronous requests, which is exactly what I wanted to do.

See Guzzle documentation for Laravel for more information.