multiprocessing: Add trivial AsyncResult, not much async at all.

pull/118/head
Paul Sokolovsky 2014-05-01 10:13:03 +03:00
rodzic b16a92a365
commit fddef71832
2 zmienionych plików z 32 dodań i 3 usunięć

Wyświetl plik

@ -62,12 +62,24 @@ def Pipe(duplex=True):
return Connection(r), Connection(w)
class AsyncResult:
def __init__(self, p, r):
self.p = p
self.r = r
def get(self):
res = self.r.recv()
self.p.join()
return res
class Pool:
def __init__(self, num):
self.num = num
def apply(self, f, args=(), kwargs={}):
def _apply(self, f, args, kwargs):
# This is pretty inefficient impl, doesn't really use pool worker
def _exec(w):
r = f(*args, **kwargs)
@ -76,6 +88,15 @@ class Pool:
p = Process(target=_exec, args=(w,))
p.register_pipe(r, w)
p.start()
r = r.recv()
return p, r
def apply(self, f, args=(), kwargs={}):
p, r = self._apply(f, args, kwargs)
res = r.recv()
p.join()
return r
return res
def apply_async(self, f, args=(), kwargs={}, callback=None, errback=None):
p, r = self._apply(f, args, kwargs)
return AsyncResult(p, r)

Wyświetl plik

@ -0,0 +1,8 @@
from multiprocessing import Pool
def f(x):
return x*x
pool = Pool(4)
future = pool.apply_async(f, (10,))
print(future.get())