Skip to content

Instantly share code, notes, and snippets.

@tlecomte
Created December 4, 2013 09:10
Show Gist options
  • Save tlecomte/7784555 to your computer and use it in GitHub Desktop.
Save tlecomte/7784555 to your computer and use it in GitHub Desktop.
Test script to be executed with Jython, that creates execnet gateways inside Java threads
from java.util.concurrent.Executors import newFixedThreadPool
from java.util.concurrent import Callable
import execnet
nThreads = 8
executorService = newFixedThreadPool(nThreads)
group = execnet.Group()
group.defaultspec = "popen//python=python"
class Task(Callable):
def __init__(self, i, group):
self.i = i
self.group = group
def call(self):
gateway = self.group.makegateway()
channel = gateway.remote_exec("""i = channel.receive(); channel.send(i)""")
channel.send(self.i)
j = channel.receive()
gateway.exit()
return j
N = 100
tasks = [Task(i, group) for i in range(N)]
futures = executorService.invokeAll(tasks)
group.terminate()
print "Results:"
for future in futures:
print future.get()
print "Finished"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment