1

特定のプロセスを複数のスレッドで実行する必要があります。php の拡​​張機能である pthreads について学びました。

たとえば、Laravel 以外の単純なスクリプトは問題なく動作し、結果は気に入りました。私は Laravel に移行することに決め、問題に直面しました。もちろん、私はグーグルで検索し、スタックオーバーフローでいくつかの質問を見つけ、そこで拡張機能の作成者に答えました。しかし、私は彼の答えを助けませんでした。

Answered Question 拡張機能の作成者。

クラス 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

ありがとうございました!

4

2 に答える 2

1
foreach ($this->pool as $w) {
   $w->start(PTHREADS_INHERIT_ALL ^ PTHREADS_INHERIT_CLASSES);
}
于 2017-01-03T07:23:42.833 に答える