Created
December 4, 2013 09:10
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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