文档没有提及@Task(Swoft\Task\Bean\Annotation\Task)的参数coroutine,但是根据源码,是支持该参数的。
该参数默认为false,配置为true时,任务执行器会通过Swoft\Task\TaskExecutor->runCoTask()执行该任务。但是由于Task进程/onTask()中不可使用协程,总是是抛出Swoole\Coroutine::create(): can't use async-io in task process.错误。
报错Task配置示例
/**
* @Task(name="TaskXXXXXX",coroutine=true)
*/
class XXXXXXTask
{
}
框架源码
/**
* The listener of swoole task
* @SwooleListener({
* SwooleEvent::ON_TASK,
* SwooleEvent::ON_FINISH,
* })
*/
class TaskEventListener implements TaskInterface, FinishInterface
{
public function onFinish(Server $server, int $taskId, $data)
{
App::trigger(TaskEvent::FINISH_TASK, $taskId, $data);
}
/**
* @param \Swoole\Server $server
* @param int $taskId
* @param int $workerId
* @param mixed $data
* @return mixed
* @throws \InvalidArgumentException
*/
public function onTask(Server $server, int $taskId, int $workerId, $data)
{
try {
/* @var TaskExecutor $taskExecutor*/
$taskExecutor = App::getBean(TaskExecutor::class);
$result = $taskExecutor->run($data);
} catch (\Throwable $throwable) {
App::error(sprintf('TaskExecutor->run %s file=%s line=%d ', $throwable->getMessage(), $throwable->getFile(), $throwable->getLine()));
$result = false;
// Release system resources
App::trigger(AppEvent::RESOURCE_RELEASE);
App::trigger(TaskEvent::AFTER_TASK);
}
return $result;
}
}
文档没有提及@Task(
Swoft\Task\Bean\Annotation\Task
)的参数coroutine
,但是根据源码,是支持该参数的。 该参数默认为false,配置为true时,任务执行器会通过Swoft\Task\TaskExecutor->runCoTask()
执行该任务。但是由于Task进程/onTask()中不可使用协程,总是是抛出Swoole\Coroutine::create(): can't use async-io in task process.
错误。报错Task配置示例
框架源码