There is a need to perform a specific process multiple threads. I learned about the extension for php - pthreads.
For example, a simple script outside Laravel works fine and I liked the results. I decided to move in Laravel, and faced with the problem. Of course I searched in google, found some questions on stackoverflow, where replied the author of extension. But me did not help his answers, so I ask you to help me.
Answered Question extension author.
There is a class App\Commands\QuestionsParserCommand. Inside I created an instance of the class App\My\Questions\QuestionsParser and call the method init(). Then the code of the method init():
// Create a pool
$pool = new Pool($this->threads, ParserWorkers::class);
// Create a thread class
$thread = new class extends Threaded
{
public function run()
{
// The class will receive data from a provider
// that will be shared between threads through ParserWorkers.
// It will work with the API and store the data in the database.
// The need to work with the threads,
// because the data for processing an incredible amount.
echo '+';
}
};
// Start a threads
for ($i = 0; $i < $this->threads; $i++) {
$pool->submit($thread);
}
$pool->shutdown();
Class ParserWorkers inherits from Worker and yet has an empty method run().
As a result, I run the script and get a message in the log of php:
[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
Information: Laravel 5.2.43, php 7.0.8, Windows
Thank you all!