| 1 |
""" |
| 2 |
example |
| 3 |
|
| 4 |
reading results from possibly blocking code running in sub processes. |
| 5 |
""" |
| 6 |
|
| 7 |
import execnet |
| 8 |
|
| 9 |
NUM_PROCESSES = 5 |
| 10 |
|
| 11 |
channels = [] |
| 12 |
for i in range(NUM_PROCESSES): |
| 13 |
gw = execnet.makegateway() |
| 14 |
channel = gw.remote_exec( |
| 15 |
""" |
| 16 |
import time |
| 17 |
secs = channel.receive() |
| 18 |
time.sleep(secs) |
| 19 |
channel.send("waited %d secs" % secs) |
| 20 |
""" |
| 21 |
) |
| 22 |
channels.append(channel) |
| 23 |
print("*** instantiated subprocess", gw) |
| 24 |
|
| 25 |
mc = execnet.MultiChannel(channels) |
| 26 |
queue = mc.make_receive_queue() |
| 27 |
|
| 28 |
print("*** verifying that timeout on receiving results from blocked subprocesses works") |
| 29 |
try: |
| 30 |
queue.get(timeout=1.0) |
| 31 |
except Exception: |
| 32 |
pass |
| 33 |
|
| 34 |
print("*** sending subprocesses some data to have them unblock") |
| 35 |
mc.send_each(1) |
| 36 |
|
| 37 |
print("*** receiving results asynchronously") |
| 38 |
for i in range(NUM_PROCESSES): |
| 39 |
channel, result = queue.get(timeout=2.0) |
| 40 |
print("result", channel.gateway, result) |