I have an application (Jupyter) which I want to launch in separate thread. The application uses PyZMQ and Tornado. Currently the application won't execute in a separate thread because the thread doesn't have a 'current' eventloop. A snippet from the application intialization function:

 def initialize(self, argv=None):
      super(IPKernelApp, self).initialize(argv)
      if self.subapp is not None:
          return

      # register zmq IOLoop with tornado
      zmq_ioloop.install()

      self.init_blackhole()
      self.init_connection_file()
      self.init_poller()
      self.init_sockets()

I have update this to:

 def initialize(self, argv=None):
      super(IPKernelApp, self).initialize(argv)
      if self.subapp is not None:
          return
      # create a new event loop for the current thread
      io_loop = ioloop.IOLoop()
      io_loop.make_current()

      # register zmq IOLoop with tornado
      zmq_ioloop.install()

      self.init_blackhole()
      self.init_connection_file()
      self.init_poller()
      self.init_sockets()

This seems to work. But maybe there are now 2 event loops. Is this the correct approach? How to get the eventloop of the main thread?

0 Answers