-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.py
More file actions
48 lines (35 loc) · 1.01 KB
/
task.py
File metadata and controls
48 lines (35 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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_func(*args, **kwargs):
print("this is a testing text")
if __name__ == '__main__':
task = Task(func=my_func)
print(task)