php - 无法从Laravel命令启动线程

标签 php multithreading laravel-5 pthreads

我是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/

相关文章:

php - 如何使用 PHP PDO 删除内存中的 SQLite 数据库?

java - 将大型论坛数据从一个系统迁移到另一个系统

javascript - Firefox 扩展的线程行为可能吗?

java - 如何在java中使用多个服务器以相同的顺序处理队列中的消息

laravel - 如何在 Laravel 中查询 created_at 字段

php - cUrl 打开多个 URL

php - 通过 PHP 在电子邮件中发送 HTML 页面

python - Python 线程是否打印到与主线程不同的缓冲区?

javascript - swal没有出现,但是数据传递了

Laravel 服务提供者从数据库检索数据