一个特定的进程需要执行多个线程。我了解了 php 的扩展 - pthreads。
例如,在 Laravel 之外的一个简单脚本运行良好,我喜欢它的结果。我决定搬到 Laravel,并面临这个问题。当然我在google上搜索,在stackoverflow上发现了一些问题,其中回复了扩展的作者。但我没有帮他回答,所以我请你帮帮我。
已回答问题扩展作者。
有一个类 App\Commands\QuestionsParserCommand。在里面,我创建了 App\My\Questions\QuestionsParser 类的一个实例,并调用了方法 init()。然后是方法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();
ParserWorkers 类继承自 Worker,但有一个空方法 run()。
结果,我运行脚本并在 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
信息:Laravel 5.2.43、php 7.0.8、Windows
谢谢大家!