W
W
werder332016-07-07 18:55:01
Laravel
werder33, 2016-07-07 18:55:01

Is it possible to run Laravel console commands on different threads?

There are two console commands check and stream. Check is the parent process and stream is the child.
I am using Reactphp.
check

public function handle()
    {
        $stream = $this->argument('stream');
      $offset = 0;
    //$v = new child();
        $loop = \React\EventLoop\Factory::create();
   
        for ($i = 1; $i <= $stream; $i++) {
    
          $process = new \React\ChildProcess\Process(Artisan::call('proxy:stream', ['stream' => $i]));
            

            $process->on('exit', function ($exitCode, $termSignal) {
                echo "Child exit\n";
            });
            $loop->addTimer(0.5, function ($timer) use ($process) {
                $process->start($timer->getLoop());
                $process->stdout->on('data', function ($output) {
                    echo "{$output}";
                });
            });

            $offset = 2 * $i;

        }

        $loop->addPeriodicTimer(5, function ($timer) {
            echo "Parent cannot be blocked by child\n";
        });
        $loop->run();

    }

stream
public function handle()
    {
        $check = new CheckProxy();
  $stream = $this->argument('stream');
        $offset = $this->argument('offset');
    
    for($i=0; $i< 10; $i++) {
      echo "Stream ".$stream." - ".$i."\n";
      sleep(1);
    }
    }

Outputs: Stream 1 - 1
Stream 1 - 2
Stream 1 - 3
....
Stream 1 - 9
Stream 2 - 1
etc.
Those. works like a regular loop and there is no question of any multithreading.
If you include in the parent process "php test.php" with the same code as stream. then everything works fine.
Can you tell me if it is possible to call console commands in different threads?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
werder33, 2016-07-08
@werder33

You need to call the child process like this:

$process = new \React\ChildProcess\Process('php artisan proxy:stream '.$i);

and everything works

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question