class PooledProcess(object): def __init__(self, proc): self.proc = proc self.reactor = proc.reactor self.client = RemotingClient( DuplexStreamTransport(self.proc.stdout, self.proc.stdin)) self.queue = set() @reactive def close(self): self.queue.clear() self.client.close() # will close the proc's stdin and stdout if not win32iocp: # can't send SIGINT on windows... let's hope the process will # notice that it's stdin has been closed self.proc.signal(signal.SIGINT) yield self.reactor.jobs.sleep(0.3) if self.proc.is_alive(): self.proc.terminate() def queue_size(self): return len(self.queue) @reactive def _wait_termination(self): yield self.proc.wait() for dfr in self.queue: if not dfr.is_set(): exc = yield WorkerProcessTerminated.from_proc( "worker process has terminated with pending requests", self.proc) dfr.throw(exc) @reactive def enqueue(self, func, args, kwargs): dfr = yield self.client._call(func, args, kwargs) dfr.register(lambda isexc, obj: self.queue.discard(dfr)) self.queue.add(dfr) rreturn(dfr)
class PooledProcess(object): def __init__(self, proc): self.proc = proc self.reactor = proc.reactor self.client = RemotingClient(DuplexStreamTransport(self.proc.stdout, self.proc.stdin)) self.queue = set() @reactive def close(self): self.queue.clear() self.client.close() # will close the proc's stdin and stdout if not win32iocp: # can't send SIGINT on windows... let's hope the process will # notice that it's stdin has been closed self.proc.signal(signal.SIGINT) yield self.reactor.jobs.sleep(0.3) if self.proc.is_alive(): self.proc.terminate() def queue_size(self): return len(self.queue) @reactive def _wait_termination(self): yield self.proc.wait() for dfr in self.queue: if not dfr.is_set(): exc = yield WorkerProcessTerminated.from_proc( "worker process has terminated with pending requests", self.proc) dfr.throw(exc) @reactive def enqueue(self, func, args, kwargs): dfr = yield self.client._call(func, args, kwargs) dfr.register(lambda isexc, obj: self.queue.discard(dfr)) self.queue.add(dfr) rreturn(dfr)
def main(reactor): reactor.jobs.schedule(10, reactor.stop) server = yield reactor.net.serve(RemotingHandler.of(MyService), 18822) client = yield RemotingClient.connect(reactor, "localhost", 18822) print client res = yield client.call("add", 33, 22) print "res is", res res = yield client.call("div", 33.0, 22.0) print "res is", res res = yield client.api.add(33, 22) print "res is", res try: res = yield client.call("div", 33, 0) except ZeroDivisionError: print "OK: got exception" else: print "Error: did not get an exception!"
def __init__(self, proc): self.proc = proc self.reactor = proc.reactor self.client = RemotingClient(DuplexStreamTransport(self.proc.stdout, self.proc.stdin)) self.queue = set()
def __init__(self, proc): self.proc = proc self.reactor = proc.reactor self.client = RemotingClient( DuplexStreamTransport(self.proc.stdout, self.proc.stdin)) self.queue = set()