Debian Codegraph @

doc/example/test_group.rst

raw
1 Managing multiple gateways and clusters
2 ==================================================
3
4 Usings Groups for managing multiple gateways
5 ------------------------------------------------------
6
7 Use ``execnet.Group`` to manage membership and lifetime of
8 multiple gateways::
9
10 >>> import execnet
11 >>> group = execnet.Group(['popen'] * 2)
12 >>> len(group)
13 2
14 >>> group
15 <Group ['gw0', 'gw1']>
16 >>> list(group)
17 [<Gateway id='gw0' receive-live, thread model, 0 active channels>, <Gateway id='gw1' receive-live, thread model, 0 active channels>]
18 >>> 'gw0' in group and 'gw1' in group
19 True
20 >>> group['gw0'] == group[0]
21 True
22 >>> group['gw1'] == group[1]
23 True
24 >>> group.terminate() # exit all member gateways
25 >>> group
26 <Group []>
27
28 Assigning gateway IDs
29 ------------------------------------------------------
30
31 All gateways are created as part of a group and receive
32 a per-group unique ``id`` after successful initialization.
33 Pass an ``id=MYNAME`` part to ``group.makegateway``. Example::
34
35 >>> import execnet
36 >>> group = execnet.Group()
37 >>> gw = group.makegateway("popen//id=sub1")
38 >>> assert gw.id == "sub1"
39 >>> group['sub1']
40 <Gateway id='sub1' receive-live, thread model, 0 active channels>
41
42 Getting (auto) IDs before instantiation
43 ------------------------------------------------------
44
45 Sometimes it's useful to know the gateway ID ahead
46 of instantiating it::
47
48 >>> import execnet
49 >>> group = execnet.Group()
50 >>> spec = execnet.XSpec("popen")
51 >>> group.allocate_id(spec)
52 >>> allocated_id = spec.id
53 >>> gw = group.makegateway(spec)
54 >>> assert gw.id == allocated_id
55
56 execnet.makegateway uses execnet.default_group
57 ------------------------------------------------------
58
59 Each time you create a gateway with ``execnet.makegateway()``
60 you actually use the ``execnet.default_group``::
61
62 >>> import execnet
63 >>> gw = execnet.makegateway()
64 >>> gw in execnet.default_group
65 True
66 >>> execnet.default_group.defaultspec # used for empty makegateway() calls
67 'popen'
68
69 Robust termination of SSH/popen processes
70 -----------------------------------------------
71
72 Use ``group.terminate(timeout)`` if you want to terminate
73 member gateways and ensure that no local subprocesses remain.
74 You can specify a ``timeout`` after which an attempt at killing
75 the related process is made::
76
77 >>> import execnet
78 >>> group = execnet.Group()
79 >>> gw = group.makegateway("popen//id=sleeper")
80 >>> ch = gw.remote_exec("import time ; time.sleep(2.0)")
81 >>> group
82 <Group ['sleeper']>
83 >>> group.terminate(timeout=1.0)
84 >>> group
85 <Group []>
86
87 execnet aims to provide totally robust termination so if
88 you have left-over processes or other termination issues
89 please :doc:`report them <../support>`. Thanks!
90
91
92 Using Groups to manage a certain type of gateway
93 ------------------------------------------------------
94
95 Set ``group.defaultspec`` to determine the default gateway
96 specification used by ``group.makegateway()``:
97
98 >>> import execnet
99 >>> group = execnet.Group()
100 >>> group.defaultspec = "ssh=localhost//chdir=mytmp//nice=20"
101 >>> gw = group.makegateway()
102 >>> ch = gw.remote_exec("""
103 ... import os.path
104 ... basename = os.path.basename(os.getcwd())
105 ... channel.send(basename)
106 ... """)
107 >>> ch.receive()
108 'mytmp'
109
110 This way a Group object becomes kind of a Gateway factory where
111 the factory-caller does not need to know the setup.