mirror of
https://github.com/sogou/workflow.git
synced 2026-02-08 01:33:17 +08:00
Change timed thread/go task's state to ERROR when timeout.
This commit is contained in:
@@ -62,7 +62,7 @@ int main(void)
|
||||
class WFTaskFactory
|
||||
{
|
||||
/* Create 'Go' task with running time limit in seconds plus nanoseconds.
|
||||
* If time exceeded, state WFT_STATE_ABORTED will be got in callback. */
|
||||
* If time exceeded, state WFT_STATE_SYS_ERROR and error ETIMEDOUT will be got in callback. */
|
||||
template<class FUNC, class... ARGS>
|
||||
static WFGoTask *create_timedgo_task(time_t seconds, long nanoseconds,
|
||||
const std::string& queue_name,
|
||||
@@ -70,7 +70,7 @@ class WFTaskFactory
|
||||
};
|
||||
~~~
|
||||
相比创建普通的go task,create_timedgo_task函数需要多传两个参数,seconds和nanoseconds。
|
||||
如果func的运行时间到达seconds+nanosconds时限,task直接callback,且state为WFT_STATE_ABORTED。
|
||||
如果func的运行时间到达seconds+nanosconds时限,task直接callback,且state为WFT_STATE_SYS_ERROR,error为ETIMEDOUT。
|
||||
注意,框架无法中断用户执行中的任务。func依然会继续执行到结束,但不会再次callback。另外,nanoseconds取值区间在\[0,10亿)。
|
||||
另外,当我们给go task加上了运行时间限制,callback的时机可能会先于func函数的结束,任务所在series可能也会先于func结束。
|
||||
如果我们在func里访问series,可能就是一个错误了。例如:
|
||||
@@ -99,7 +99,7 @@ int main()
|
||||
{
|
||||
...
|
||||
}
|
||||
else // state == WFT_STATE_ABORTED. // 超过运行时间限制
|
||||
else // state == WFT_STATE_SYS_ERROR && error == ETIMEDOUT // 超过运行时间限制
|
||||
{
|
||||
...
|
||||
}
|
||||
@@ -120,7 +120,7 @@ int main()
|
||||
{
|
||||
int result = (int)task->user_data;
|
||||
}
|
||||
else // state == WFT_STATE_ABORTED. // 超过运行时间限制
|
||||
else // state == WFT_STATE_SYS_ERROR && error == ETIMEDOUT // 超过运行时间限制
|
||||
{
|
||||
...
|
||||
}
|
||||
|
||||
@@ -57,14 +57,14 @@ You may create a go task with running time limit by calling WFTaskFactory::creat
|
||||
class WFTaskFactory
|
||||
{
|
||||
/* Create 'Go' task with running time limit in seconds plus nanoseconds.
|
||||
* If time exceeded, state WFT_STATE_ABORTED will be got in callback. */
|
||||
* If time exceeded, state WFT_STATE_SYS_ERROR and error ETIMEDOUT will be got in callback. */
|
||||
template<class FUNC, class... ARGS>
|
||||
static WFGoTask *create_timedgo_task(time_t seconds, long nanoseconds,
|
||||
const std::string& queue_name,
|
||||
FUNC&& func, ARGS&&... args);
|
||||
};
|
||||
~~~
|
||||
Compared with creating a normal go task, the ``create_timedgo_task`` function needs to pass two more parameters, seconds and nanoseconds. If the running time of ``func`` reaches the seconds+nanosconds time limit, the task callback directly, and the state is WFT_STATE_ABORTED.
|
||||
Compared with creating a normal go task, the ``create_timedgo_task`` function needs to pass two more parameters, seconds and nanoseconds. If the running time of ``func`` reaches the seconds+nanosconds time limit, the task callback directly, and the state is WFT_STATE_SYS_ERROR and the error is ETIMEDOUT.
|
||||
Note that the framework cannot interrupt the user's ongoing task. ``func`` will still continue to execute to the end, but will not callback again. In addition, the value range of nanoseconds is [0,1 billion).
|
||||
|
||||
|
||||
|
||||
@@ -172,7 +172,7 @@ Obviously, our framework can not interrupt a computing task because it's a user
|
||||
...
|
||||
};
|
||||
~~~
|
||||
This create_thread_task function needs to pass two more parameters, seconds and nanoseconds. If the running time of func reaches the seconds+nanosconds time limit, the task callback directly, and the state is WFT_STATE_ABORTED. But the task routine will continue to run till the end.
|
||||
This create_thread_task function needs to pass two more parameters, seconds and nanoseconds. If the running time of func reaches the seconds+nanosconds time limit, the task callback directly, and the state is WFT_STATE_SYS_ERROR and the error is ETIMEDOUT. But the task routine will continue to run till the end.
|
||||
|
||||
# Symmetry of the algorithm and the protocol
|
||||
|
||||
|
||||
@@ -164,7 +164,7 @@ public:
|
||||
};
|
||||
~~~
|
||||
参数seconds和nanoseconds构成了运行时限。在这里,nanoseconds的取值范围在\[0,1000000000)。
|
||||
当任务无法在运行时限内结束,会直接回到callback,并且任务的状态为WFT_STATE_ABORTED。
|
||||
当任务无法在运行时限内结束,会直接回到callback,并且任务的状态为WFT_STATE_SYS_ERROR且错误码为ETIMEDOUT。
|
||||
还是用matrix_multiply的例子,我们可以这样写:
|
||||
~~~cpp
|
||||
void callback(MMTask *task) // MMtask = WFThreadTask<MMInput, MMOutput>
|
||||
@@ -172,12 +172,12 @@ void callback(MMTask *task) // MMtask = WFThreadTask<MMInput, MMOutput>
|
||||
MMInput *input = task->get_input();
|
||||
MMOutput *output = task->get_output();
|
||||
|
||||
if (task->get_state() == WFT_STATE_ABORTED)
|
||||
if (task->get_state() == WFT_STATE_SYS_ERROR && task->get_error() == ETIMEDOUT)
|
||||
{
|
||||
printf("Run out of time.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
assert(task->get_state() == WFT_STATE_SUCCESS)
|
||||
|
||||
if (output->error)
|
||||
@@ -210,13 +210,13 @@ int main()
|
||||
...
|
||||
}
|
||||
~~~
|
||||
上面的示例,限制了任务运行时间不超过1毫秒,否则,以WFT_STATE_ABORTD的状态返回。
|
||||
上面的示例,限制了任务运行时间不超过1毫秒,否则,以WFT_STATE_SYS_ERROR的状态返回。
|
||||
再次提醒,我们并不会中断用户的实际运行函数。当任务超时并callback,计算函数还会一直运行直到结束。
|
||||
如果用户希望函数不再继续执行,需要在代码中自行加入检查点来实现这样的功能。可以在INPUT里加入flag,例如:
|
||||
~~~cpp
|
||||
void callback(MMTask *task) // MMtask = WFThreadTask<MMInput, MMOutput>
|
||||
{
|
||||
if (task->get_state() == WFT_STATE_ABORTED)
|
||||
if (task->get_state() == WFT_STATE_SYS_ERROR && task->get_error() == ETIMEDOUT)
|
||||
{
|
||||
task->get_input()->flag = true;
|
||||
printf("Run out of time.\n");
|
||||
|
||||
Reference in New Issue
Block a user