kopia lustrzana https://github.com/micropython/micropython-lib
multiprocessing: tests: Turn into proper tests, make CPython compatible.
rodzic
3e3c6fcaa6
commit
13a6f4955c
|
@ -1,6 +1,6 @@
|
|||
import sys
|
||||
import os
|
||||
from multiprocessing import Process, Pipe, Connection
|
||||
from multiprocessing import Process, Pipe
|
||||
|
||||
def f(conn):
|
||||
conn.send([42, None, 'hello'])
|
||||
|
@ -9,11 +9,14 @@ def f(conn):
|
|||
|
||||
if __name__ == '__main__':
|
||||
parent_conn, child_conn = Pipe(False)
|
||||
print(parent_conn, child_conn)
|
||||
#print(parent_conn, child_conn)
|
||||
p = Process(target=f, args=(child_conn,))
|
||||
|
||||
# Extension: need to call this for uPy
|
||||
p.register_pipe(parent_conn, child_conn)
|
||||
if sys.implementation.name == "micropython":
|
||||
p.register_pipe(parent_conn, child_conn)
|
||||
|
||||
p.start()
|
||||
print(parent_conn.recv())
|
||||
print(parent_conn.recv())
|
||||
parent_conn.recv() == [42, None, 'hello']
|
||||
parent_conn.recv() == [42, 42, 42]
|
||||
p.join()
|
||||
|
|
|
@ -4,4 +4,4 @@ def f(x):
|
|||
return x*x
|
||||
|
||||
pool = Pool(4)
|
||||
print(pool.apply(f, (10,)))
|
||||
assert pool.apply(f, (10,)) == 100
|
||||
|
|
|
@ -6,19 +6,21 @@ def f(x):
|
|||
|
||||
pool = Pool(4)
|
||||
future = pool.apply_async(f, (10,))
|
||||
print(future.get())
|
||||
assert future.get() == 100
|
||||
|
||||
def f2(x):
|
||||
time.sleep(1)
|
||||
time.sleep(0.5)
|
||||
return x + 1
|
||||
|
||||
future = pool.apply_async(f2, (10,))
|
||||
iter = 0
|
||||
while not future.ready():
|
||||
print("not ready")
|
||||
time.sleep(0.2)
|
||||
|
||||
print(future.get())
|
||||
#print("not ready")
|
||||
time.sleep(0.1)
|
||||
iter += 1
|
||||
|
||||
assert future.get() == 11
|
||||
assert iter >= 5 and iter <= 8
|
||||
|
||||
t = time.time()
|
||||
futs = [
|
||||
|
@ -27,6 +29,7 @@ futs = [
|
|||
pool.apply_async(f2, (12,)),
|
||||
]
|
||||
|
||||
iter = 0
|
||||
while True:
|
||||
#not all(futs):
|
||||
c = 0
|
||||
|
@ -35,7 +38,10 @@ while True:
|
|||
c += 1
|
||||
if not c:
|
||||
break
|
||||
print("not ready2")
|
||||
time.sleep(0.2)
|
||||
#print("not ready2")
|
||||
time.sleep(0.1)
|
||||
iter += 1
|
||||
|
||||
assert iter >= 5 and iter <= 8
|
||||
|
||||
print("Run 3 parallel sleep(1)'s in: ", time.time() - t)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
from multiprocessing import Process
|
||||
|
||||
def f(name):
|
||||
print('hello', name)
|
||||
assert name == 'bob'
|
||||
|
||||
if __name__ == '__main__':
|
||||
p = Process(target=f, args=('bob',))
|
||||
|
|
Ładowanie…
Reference in New Issue