Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# -*- coding: utf-8 -*-
import execnet
def multiplier(channel, factor):
while not channel.isclosed():
param = channel.receive()
channel.send(param * factor)
gw = execnet.makegateway()
channel = gw.remote_exec(multiplier, factor=10)
for i in range(5):
channel.send(i)
result = channel.receive()
assert result == i * 10
gw.exit()
def test_hrsync_one_host(self, mysetup):
source, dest = mysetup.source, mysetup.dest
gw = execnet.makegateway("popen//chdir=%s" % dest)
finished = []
rsync = HostRSync(source)
rsync.add_target_host(gw, finished=lambda: finished.append(1))
source.join("hello.py").write("world")
rsync.send()
gw.exit()
assert dest.join(source.basename, "hello.py").check()
assert len(finished) == 1
def test_hrsync_one_host(self, mysetup):
source, dest = mysetup.source, mysetup.dest
gw = execnet.makegateway("popen//chdir=%s" % dest)
finished = []
rsync = HostRSync(source)
rsync.add_target_host(gw, finished=lambda: finished.append(1))
source.join("hello.py").write("world")
rsync.send()
gw.exit()
assert dest.join(source.basename, "hello.py").check()
assert len(finished) == 1
def getgateway(host, configfile=None):
xspec = "ssh=%s" % host
if configfile is not None:
xspec += "//ssh_config=%s" % configfile
return execnet.makegateway(xspec)
# -*- coding: utf-8 -*-
"""
example
reading results from possibly blocking code running in sub processes.
"""
from __future__ import print_function
import execnet
NUM_PROCESSES = 5
channels = []
for i in range(NUM_PROCESSES):
gw = execnet.makegateway() # or use SSH or socket gateways
channel = gw.remote_exec(
"""
import time
secs = channel.receive()
time.sleep(secs)
channel.send("waited %d secs" % secs)
"""
)
channels.append(channel)
print("*** instantiated subprocess", gw)
mc = execnet.MultiChannel(channels)
queue = mc.make_receive_queue()
print("*** verifying that timeout on receiving results from blocked subprocesses works")
try:
def setup(self,):
self.testdir.chdir()
# import os ; os.environ['EXECNET_DEBUG'] = "2"
self.gateway = execnet.makegateway()
self.config = config = self.testdir.parseconfigure()
putevent = self.use_callback and self.events.put or None
class DummyMananger:
testrunuid = uuid.uuid4().hex
specs = [0, 1]
self.slp = WorkerController(DummyMananger, self.gateway, config, putevent)
self.request.addfinalizer(self.slp.ensure_teardown)
self.slp.setup()
# -*- coding: utf-8 -*-
import execnet
gw = execnet.makegateway("popen//python=python2")
channel = gw.remote_exec(
"""
import numpy
array = numpy.array([1,2,3])
while 1:
x = channel.receive()
if x is None:
break
array = numpy.append(array, x)
channel.send(repr(array))
"""
)
for x in range(10):
channel.send(x)
channel.send(None)
print(channel.receive())
def initgateway(self):
return execnet.makegateway("popen")
def test_2_to_3_bridge_can_send_binary_files(self, tmpdir):
python = _find_version('3')
gw = execnet.makegateway('popen//python=%s'%(python,))
source = tmpdir.ensure('source', dir=1)
for i, content in enumerate('foo bar baz \x10foo'):
source.join(str(i)).write(content)
rsync = RSync(source)
target = tmpdir.join('target')
rsync.add_target(gw, target)
rsync.send()
def ff(*args):
"""
function_name: str=args[0]
Code adapted from https://stackoverflow.com/a/44965570
"""
gw = execnet.makegateway("popen//python=" + FF_PYTHON)
channel = gw.remote_exec("""
from {} import {} as the_function
channel.send(the_function(*channel.receive()))
""".format(MODULE, args[0]))
channel.send(args[1:])
return channel.receive()