| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import execnet |
| 4 |
|
| 5 |
group = execnet.Group() |
| 6 |
for i in range(4): |
| 7 |
group.makegateway() |
| 8 |
|
| 9 |
|
| 10 |
def process_item(channel): |
| 11 |
|
| 12 |
import time |
| 13 |
import random |
| 14 |
|
| 15 |
channel.send("ready") |
| 16 |
for x in channel: |
| 17 |
if x is None: |
| 18 |
break |
| 19 |
|
| 20 |
time.sleep(random.randrange(3)) |
| 21 |
channel.send(x * 10) |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
mch = group.remote_exec(process_item) |
| 26 |
|
| 27 |
|
| 28 |
q = mch.make_receive_queue(endmarker=-1) |
| 29 |
tasks: list[int] | None = list(range(10)) |
| 30 |
terminated = 0 |
| 31 |
while 1: |
| 32 |
channel, item = q.get() |
| 33 |
if item == -1: |
| 34 |
terminated += 1 |
| 35 |
print("terminated %s" % channel.gateway.id) |
| 36 |
if terminated == len(mch): |
| 37 |
print("got all results, terminating") |
| 38 |
break |
| 39 |
continue |
| 40 |
if item != "ready": |
| 41 |
print(f"other side {channel.gateway.id} returned {item!r}") |
| 42 |
if not tasks and tasks is not None: |
| 43 |
print("no tasks remain, sending termination request to all") |
| 44 |
mch.send_each(None) |
| 45 |
tasks = None |
| 46 |
if tasks: |
| 47 |
task = tasks.pop() |
| 48 |
channel.send(task) |
| 49 |
print(f"sent task {task!r} to {channel.gateway.id}") |
| 50 |
|
| 51 |
group.terminate() |