diff --git a/multiprocessing/multiprocessing.py b/multiprocessing/multiprocessing.py index 524603bd..2c39bec3 100644 --- a/multiprocessing/multiprocessing.py +++ b/multiprocessing/multiprocessing.py @@ -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: diff --git a/multiprocessing/test_pool_async.py b/multiprocessing/test_pool_async.py index 3fb047c3..f3796a7f 100644 --- a/multiprocessing/test_pool_async.py +++ b/multiprocessing/test_pool_async.py @@ -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())