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