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()
|