-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.py
44 lines (34 loc) · 1.07 KB
/
job.py
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
import concurrent
import queue
def callback_done(aloop, ajob):
def _clear(fut):
if fut.done():
try:
aloop.running.remove(ajob)
pending_ajob = aloop.pending.get_nowait()
except KeyError:
pass
except queue.Empty:
pass
else:
aloop.submit(pending_ajob.job_coro, pending_ajob.callback)
return _clear
class AsyncJob:
"""A simple wrapper for concurrent.Future, currently not awaitable"""
def __init__(self, job_coro, callback=None, future=None):
self.job_coro = job_coro
self.callback = callback
self.future = future or concurrent.futures.Future()
@property
def state(self):
return self.future._state
def result(self):
return self.future.result()
def exception(self):
return self.future.exception()
def cancel(self):
return self.future.cancel()
def done(self):
return self.future.done()
def cancelled(self):
return self.future.cancelled()