我是php中的pthreads的新手。我了解到不可能通过网络服务器从apache启动它,所以我不得不使用laravel命令从命令行运行它,但是当我调用
开始()
线程方法
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
但是只要我调用run方法,错误就消失了,这不是解决方法。
我会很感激我能得到的任何帮助。
我命令的handle方法就这样
public function handle()
{
$threads = ["Thread One", "Thread Two"];
$threadList=[];
foreach ($threads as &$thread){
//$id++;
$this->info($thread);
$testThread = new TestThread($thread);
$testThread->start();
$threadList[] = $testThread;
}
foreach ($threadList as $thread){
$this->info("waiting for thread ". $thread->getThreadID());
$thread->join();
$this->info($thread->getThreadID()."done ");
//echo $thread->getThreadID().'<br/>';
}
}
而我的ThreadClass是这个
namespace App\Engine\Threads\ThreadClass;
use Illuminate\Support\Facades\Log;
class TestThread extends \Thread
{
private $threadID;
public function __construct($threadID)
{
$this->threadID = $threadID;
}
/**
* @return mixed
*/
public function getThreadID()
{
return $this->threadID;
}
public function run()
{
$this->threadID = $this->threadID.''.mt_rand(4000, 5000);
}
}
最佳答案
我通过将以下参数添加到start函数中找到了解决问题的方法
$testThread->start(PTHREADS_INHERIT_ALL ^ PTHREADS_INHERIT_CLASSES);
由这个问题提供Fatal error while starting threads within symfony command
关于php - 无法从Laravel命令启动线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39720183/