def run(self): try: logger.debug("in thread") # data storage self.clients = set() self.plots = OrderedDict() # tornado and zmq play nice? self.ioloop = ZMQIOLoop() # zmq self.context = zmq.Context() self.socket_in = self.context.socket(zmq.SUB) self.socket_in.connect("tcp://0.0.0.0:"+str(self.zmq_port)) self.socket_in.setsockopt_string(zmq.SUBSCRIBE, "") # input handler self.stream = zmqstream.ZMQStream(self.socket_in, io_loop=self.ioloop) self.stream.on_recv(self.on_recv) # tornado self.application = Application(io_loop=self.ioloop) self.application._server = self self.server = HTTPServer(self.application) self.server.listen(self.http_port) logger.debug("starting IOLoop") self.ioloop.start() logger.debug("done thread") except Exception as e: # capture exceptions from daemonic thread to log file import traceback as tb logger.error("Exception in server thread:\n" + str(e) + str(tb.format_exc()))
def perform(self, request): def on_timeout(): io_loop.remove_timeout(to) self.pool.put_stream(stream) raise TimeoutError("timeout") def on_response(message): io_loop.remove_timeout(to) self.pool.put_stream(stream) response = ResponseFactory().loads(message[0]) if response.error == ERROR_EXCEPTION: raise response.result future.set_result(response) future = Future() stream = self.pool.get_stream() stream.send(RequestFactory().dumps(request)) io_loop = ZMQIOLoop().instance() wait_time = datetime.timedelta(seconds=2) to = io_loop.add_timeout(wait_time, on_timeout) stream.on_recv(on_response) return future
def start_ipython(ip=None, ns=None, log=None): """Start an IPython kernel in a thread Parameters ---------- ip: str The IP address to listen on (likely the parent object's ip). ns: dict Any names that should be injected into the IPython namespace. log: logger instance Hook up IPython's logging to an existing logger instead of the default. """ from IPython import get_ipython if get_ipython() is not None: raise RuntimeError("Cannot start IPython, it's already running.") from zmq.eventloop.ioloop import ZMQIOLoop from ipykernel.kernelapp import IPKernelApp # save the global IOLoop instance # since IPython relies on it, but we are going to put it in a thread. save_inst = IOLoop.instance() IOLoop.clear_instance() zmq_loop = ZMQIOLoop() zmq_loop.install() # start IPython, disabling its signal handlers that won't work due to running in a thread: app = IPKernelApp.instance(log=log) # Don't connect to the history database app.config.HistoryManager.hist_file = ":memory:" # listen on all interfaces, so remote clients can connect: if ip: app.ip = ip # disable some signal handling, logging def noop(): return None app.init_signal = noop app.log_connection_info = noop # start IPython in a thread # initialization happens in the thread to avoid threading problems # with the sqlite history evt = Event() def _start(): app.initialize([]) app.kernel.pre_handler_hook = noop app.kernel.post_handler_hook = noop app.kernel.start() app.kernel.loop = IOLoop.instance() # save self in the IPython namespace as 'worker' # inject things into the IPython namespace if ns: app.kernel.shell.user_ns.update(ns) evt.set() zmq_loop.start() zmq_loop_thread = Thread(target=_start) zmq_loop_thread.daemon = True zmq_loop_thread.start() assert evt.wait(timeout=5), "IPython didn't start in a reasonable amount of time." # put the global IOLoop instance back: IOLoop.clear_instance() save_inst.install() return app
json.dumps( dict(status='error', message='Session error,please refresh'))) self.finish() if __name__ == '__main__': options.define("p", default=7777, help="run on the given port", type=int) options.parse_command_line() config = Configurations() port = options.options.p logger = get_logger("server", logging.DEBUG) loop = ZMQIOLoop() loop.install() context = zmq.Context() zmq_publish = context.socket(zmq.PUB) zmq_publish.bind("tcp://127.0.0.1:%s" % str(config.get_configuration("zmqPublish"))) zmq_dispatch = context.socket(zmq.REP) zmq_dispatch.bind("tcp://127.0.0.1:%s" % str(config.get_configuration("zmqDispatch"))) zmq_result = context.socket(zmq.PULL) zmq_result.bind("tcp://127.0.0.1:%s" % str(config.get_configuration("zmqResult"))) receiver = ZMQStream(zmq_result) receiver.on_recv(on_worker_data_in) cli_device_dict = {}
def __init__(self): self.ioloop = ZMQIOLoop() self.ioloop.install() return
def __init__(self): self.ioloop = ZMQIOLoop() self.ioloop.install() self.clients = {} self.in_client = "server" return