实现基本任务对象Task(Python)
任务处理逻辑
- 任务参数
- 任务唯一标记(UUID)
- 任务具体的执行逻辑
具体实现(Python)
# -*- encoding=utf-8 -*-
import uuid
import threading
# 基本任务对象
class Task:
def __init__(self, func, *args, **kwargs):
# 任务具体逻辑,通过函数引用传递进来
self.callable = func
self.id = uuid.uuid4()
self.args = args
self.kwargs = kwargs
def __str__(self):
return 'Task id: ' + str(self.id)
# 异步任务对象
class AsyncTask(Task):
def __init__(self, func, *args, **kwargs):
self.result = None
self.condition = threading.Condition()
super().__init__(func, *args, **kwargs)
# 设置运行结果
def set_result(self, result):
self.condition.acquire()
self.result = result
self.condition.notify()
self.condition.release()
# 获取任务结果
def get_result(self):
self.condition.acquire()
if not self.result:
self.condition.wait()
result = self.result
self.condition.release()
return result
def my_function():
print('this is a task test.')
if __name__ == '__main__':
task = Task(func=my_function)
print(task)