kopia lustrzana https://github.com/micropython/micropython-lib
multiprocessing: Add AsyncResult.ready() with real async implementation.
rodzic
f94297ff86
commit
cd2774bb8e
|
@ -1,5 +1,6 @@
|
||||||
import os
|
import os
|
||||||
import pickle
|
import pickle
|
||||||
|
import select
|
||||||
|
|
||||||
|
|
||||||
class Process:
|
class Process:
|
||||||
|
@ -67,12 +68,22 @@ class AsyncResult:
|
||||||
def __init__(self, p, r):
|
def __init__(self, p, r):
|
||||||
self.p = p
|
self.p = p
|
||||||
self.r = r
|
self.r = r
|
||||||
|
self.ep = None
|
||||||
|
|
||||||
def get(self):
|
def get(self):
|
||||||
res = self.r.recv()
|
res = self.r.recv()
|
||||||
self.p.join()
|
self.p.join()
|
||||||
return res
|
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:
|
class Pool:
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import time
|
||||||
from multiprocessing import Pool
|
from multiprocessing import Pool
|
||||||
|
|
||||||
def f(x):
|
def f(x):
|
||||||
|
@ -6,3 +7,14 @@ def f(x):
|
||||||
pool = Pool(4)
|
pool = Pool(4)
|
||||||
future = pool.apply_async(f, (10,))
|
future = pool.apply_async(f, (10,))
|
||||||
print(future.get())
|
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())
|
||||||
|
|
Ładowanie…
Reference in New Issue