看了TY大佬(@Tinywan)的公眾號文章,有個spatie/async 的使用;
https://github.com/spatie/async
https://mp.weixin.qq.com/s/sd3leq0ezl9ssPryUHQ18A
我是在thinkphp5.1里面使用;
但是報錯了,如果不使用TP的相關(guān)的類是可以正常跑的,在子任務(wù)里面用了TP的相關(guān)類就找不到類了;
我看文檔是子任務(wù)要初始化類,但是我應(yīng)該怎么把TP的類注入到子任務(wù)呢?
我看Laravel 中是下面這樣操作,TP的應(yīng)該怎么操作呢?
$pool[] = async(function () use ($result, $syncemployRepository) {
$app = require __DIR__.'/../../../../bootstrap/app.php';
$kernel = $app->make(\Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();
// 任務(wù)代碼
})->then(function ($output) use($pool) {
$pool->stop();
})->catch(function (Exception $e) {
Log::error('createauth_sync', ['msg' => $e->getMessage()]);
});
$pool = Pool::create()
->concurrency(20)
->autoload(__DIR__ . '/../../../vendor/autoload.php');
foreach ($cursor as $user) {
$pool[] = async(function () use ($user) {
return self::syncUp($user);
})->then(function (string $output) use ($user) {
Log::record($output);
})->catch(function (\Throwable $e) use ($user) {
Log::record($e->getMessage());
exit();
});
}
await($pool);
public static function syncUp($user): string
{
return Db::name('user')->where('id',$user['id'])->value('name');
}