N
N
Nik Gubin2016-10-13 13:41:29
Laravel
Nik Gubin, 2016-10-13 13:41:29

Pthreads, Laravel Commands and Closure - How to fix Serialization of 'Closure' is not allowed?

Good afternoon, comrades.
There is a need to execute a certain process in several threads. Discovered an extension for php - pthreads.
For a simple script outside of Laravel, everything works great and I really liked the results. I decided to migrate to Laravel and ran into a problem. Of course, I searched the net, found several questions on stackoverflow, where the author of the extension himself answered. But his answers did not help me, so I ask you to help me.
Question with the answer of the author of the extension.
There is a class App\Commands\QuestionsParserCommand. Inside, I create an instance of the App\My\Questions\QuestionsParser class and call the init method. Here is the code for the init() method:

// Создадим пул
$pool = new Pool($this->threads, ParserWorkers::class);

// Создадим класс потока
$thread = new class extends Threaded
{
  public function run()
  {
    // Класс будет получать определённые данные из некого провайдера,
    // который будет распределяться между потоками через ParserWorkers
    // Работать с API и сохранять данные в БД.
    // Необходима работа с потоками, ибо данных для обработки невероятно много

    echo '+';
  }
};

// Запустим потоки
for ($i = 0; $i < $this->threads; $i++) {
  $pool->submit($thread);
}

$pool->shutdown();

The ParserWorkers class inherits from Worker and so far has an empty run() method.
As a result, I run the script and get a message in the logs:
[13-Oct-2016 11:27:35 Europe/Moscow] 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

Details:
Laravel 5.2.43, php 7.0.8, Windows
PS Please don't suggest Laravel Task Scheduler or Queues, I remember them well but they don't really suit my task.
Thanks for any help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
romy4, 2016-10-13
@romy4

You need to create a non-anonymous class

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question