Debian Codegraph @

doc/example/redirect_remote_output.py

raw symbols SCIP
1 """
2 redirect output from remote to a local function
3 showcasing features of the channel object:
4
5 - sending a channel over a channel
6 - adapting a channel to a file object
7 - setting a callback for receiving channel data
8
9 """
10
11 import execnet
12
13 gw = execnet.makegateway()
14
15 outchan = gw.remote_exec(
16 """
17 import sys
18 outchan = channel.gateway.newchannel()
19 sys.stdout = outchan.makefile("w")
20 channel.send(outchan)
21 """
22 ).receive()
23
24
25 # note: callbacks execute in receiver thread!
26 def write(data):
27 print("received:", repr(data))
28
29
30 outchan.setcallback(write) # type: ignore[attr-defined]
31
32 gw.remote_exec(
33 """
34 print('hello world')
35 print('remote execution ends')
36 """
37 ).waitclose()