multiprocessing: Add AsyncResult.ready() with real async implementation.

pull/118/head
Paul Sokolovsky 2014-05-01 10:30:01 +03:00
rodzic f94297ff86
commit cd2774bb8e
2 zmienionych plików z 23 dodań i 0 usunięć

Wyświetl plik

@ -1,5 +1,6 @@
import os
import pickle
import select
class Process:
@ -67,12 +68,22 @@ class AsyncResult:
def __init__(self, p, r):
self.p = p
self.r = r
self.ep = None
def get(self):
res = self.r.recv()
self.p.join()
return res
def ready(self):
if not self.ep:
self.ep = select.epoll()
self.ep.register(self.r.f.fileno(), select.EPOLLIN, None)
res = self.ep.poll(0)
if res:
self.ep.close()
return bool(res)
class Pool:

Wyświetl plik

@ -1,3 +1,4 @@
import time
from multiprocessing import Pool
def f(x):
@ -6,3 +7,14 @@ def f(x):
pool = Pool(4)
future = pool.apply_async(f, (10,))
print(future.get())
def f2(x):
time.sleep(1)
return x + 1
future = pool.apply_async(f2, (10,))
while not future.ready():
print("not ready")
time.sleep(0.1)
print(future.get())