forked from pytest-dev/execnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopen_read_multiple.py
More file actions
40 lines (32 loc) · 954 Bytes
/
Copy pathpopen_read_multiple.py
File metadata and controls
40 lines (32 loc) · 954 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
example
reading results from possibly blocking code running in sub processes.
"""
import execnet
NUM_PROCESSES = 5
channels = []
for i in range(NUM_PROCESSES):
gw = execnet.makegateway() # or use SSH or socket gateways
channel = gw.remote_exec(
"""
import time
secs = channel.receive()
time.sleep(secs)
channel.send("waited %d secs" % secs)
"""
)
channels.append(channel)
print("*** instantiated subprocess", gw)
mc = execnet.MultiChannel(channels)
queue = mc.make_receive_queue()
print("*** verifying that timeout on receiving results from blocked subprocesses works")
try:
queue.get(timeout=1.0)
except Exception:
pass
print("*** sending subprocesses some data to have them unblock")
mc.send_each(1)
print("*** receiving results asynchronously")
for i in range(NUM_PROCESSES):
channel, result = queue.get(timeout=2.0)
print("result", channel.gateway, result)