Answer the question
In order to leave comments, you need to log in
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();
[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
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question