File: redirect_remote_output.py

package info (click to toggle)
execnet 2.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 684 kB
  • sloc: python: 5,244; makefile: 78; sh: 2
file content (37 lines) | stat: -rw-r--r-- 702 bytes parent folder | download
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
"""
redirect output from remote to a local function
showcasing features of the channel object:

- sending a channel over a channel
- adapting a channel to a file object
- setting a callback for receiving channel data

"""

import execnet

gw = execnet.makegateway()

outchan = gw.remote_exec(
    """
    import sys
    outchan = channel.gateway.newchannel()
    sys.stdout = outchan.makefile("w")
    channel.send(outchan)
"""
).receive()


# note: callbacks execute in receiver thread!
def write(data):
    print("received:", repr(data))


outchan.setcallback(write)  # type: ignore[attr-defined]

gw.remote_exec(
    """
    print('hello world')
    print('remote execution ends')
"""
).waitclose()