-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask.py
More file actions
49 lines (41 loc) · 1.29 KB
/
Task.py
File metadata and controls
49 lines (41 loc) · 1.29 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
49
import sys
from threading import Thread, Lock, Event
from functools import wraps
PY3 = sys.version_info[0] == 3
if PY3:
from queue import Queue
else:
from Queue import Queue
def task(func):
@wraps(func)
def wrapper(self, *args, **kwargs):
name = '_'+func.__name__
if not getattr(self.__class__, name, None): setattr(self.__class__,name,func)
id = kwargs.get('id', '')
self._task.put((name, str(id))+args)
return wrapper
import Pyro4
@Pyro4.expose
class Task:
def __init__(self):
self._task = Queue()
self._last = {}
self._lock = Lock()
self._quit = Event()
self._worker = Thread(target=self._run)
self._worker.setDaemon(True)
self._worker.start()
def _run(self):
while not self._quit.is_set():
call = self._task.get()
result = getattr(self, call[0])(*call[2:])
with self._lock:
self._last[call[0]+call[1]] = result
self._task.task_done()
def wait(self, timeout = 0):
self._quit.wait(timeout)
def quit(self):
self._quit.set()
def get(self, item):
with self._lock:
return self._last.get('_'+item)