Debian Codegraph @

doc/example/test_multi.rst

raw
1 Advanced (multi) channel communication
2 =====================================================
3
4 MultiChannel: container for multiple channels
5 ------------------------------------------------------
6
7 Use ``execnet.MultiChannel`` to work with multiple channels::
8
9 >>> import execnet
10 >>> ch1 = execnet.makegateway().remote_exec("channel.send(1)")
11 >>> ch2 = execnet.makegateway().remote_exec("channel.send(2)")
12 >>> mch = execnet.MultiChannel([ch1, ch2])
13 >>> len(mch)
14 2
15 >>> mch[0] is ch1 and mch[1] is ch2
16 True
17 >>> ch1 in mch and ch2 in mch
18 True
19 >>> sum(mch.receive_each())
20 3
21
22 Receive results from sub processes with a Queue
23 -----------------------------------------------------
24
25 Use ``MultiChannel.make_receive_queue()`` to get a queue
26 from which to obtain results::
27
28 >>> ch1 = execnet.makegateway().remote_exec("channel.send(1)")
29 >>> ch2 = execnet.makegateway().remote_exec("channel.send(2)")
30 >>> mch = execnet.MultiChannel([ch1, ch2])
31 >>> queue = mch.make_receive_queue()
32 >>> chan1, res1 = queue.get()
33 >>> chan2, res2 = queue.get(timeout=3)
34 >>> res1 + res2
35 3
36
37 Working asynchronously/event-based with channels
38 ---------------------------------------------------
39
40 Use channel callbacks if you want to process incoming
41 data immediately and without blocking execution::
42
43 >>> import execnet
44 >>> gw = execnet.makegateway()
45 >>> ch = gw.remote_exec("channel.receive() ; channel.send(42)")
46 >>> l = []
47 >>> ch.setcallback(l.append)
48 >>> ch.send(1)
49 >>> ch.waitclose()
50 >>> assert l == [42]
51
52 Note that the callback function will be executed in the
53 receiver thread and should not block or run for too long.
54
55 Robustly receive results and termination notification
56 -----------------------------------------------------
57
58 Use ``MultiChannel.make_receive_queue(endmarker)`` to specify
59 an object to be put to the queue when the remote side of a channel
60 is closed. The endmarker will also be put to the Queue if the gateway
61 is blocked in execution and is terminated/killed::
62
63 >>> group = execnet.Group(['popen'] * 3) # create three gateways
64 >>> mch = group.remote_exec("channel.send(channel.receive()+1)")
65 >>> queue = mch.make_receive_queue(endmarker=42)
66 >>> mch[0].send(1)
67 >>> chan1, res1 = queue.get()
68 >>> res1
69 2
70 >>> group.terminate(timeout=1) # kill processes waiting on receive
71 >>> for i in range(3):
72 ... chan1, res1 = queue.get()
73 ... assert res1 == 42
74 >>> group
75 <Group []>
76
77
78
79 Saturate multiple Hosts and CPUs with tasks to process
80 --------------------------------------------------------
81
82 If you have multiple CPUs or hosts you can create as many
83 gateways and then have a process sit on each CPU and wait
84 for a task to proceed. One complication is that we
85 want to ensure clean termination of all processes
86 and loose no result. Here is an example that just uses
87 local subprocesses and does the task:
88
89 .. include:: taskserver.py
90 :literal: