Python tornado.ioloop() Examples

The following are 30 code examples of tornado.ioloop(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tornado , or try the search function .
Example #1
Source File: twisted.py    From honeything with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, io_loop=None):
        if not io_loop:
            io_loop = tornado.ioloop.IOLoop.instance()
        self._io_loop = io_loop
        self._readers = {}  # map of reader objects to fd
        self._writers = {}  # map of writer objects to fd
        self._fds = {}  # a map of fd to a (reader, writer) tuple
        self._delayedCalls = {}
        PosixReactorBase.__init__(self)

        # IOLoop.start() bypasses some of the reactor initialization.
        # Fire off the necessary events if they weren't already triggered
        # by reactor.run().
        def start_if_necessary():
            if not self._started:
                self.fireSystemEvent('startup')
        self._io_loop.add_callback(start_if_necessary)

    # IReactorTime 
Example #2
Source File: http_download.py    From honeything with GNU General Public License v3.0 6 votes vote down vote up
def _start_download(self):
    #print 'starting (auth_header=%r)' % self.auth_header
    ht.logger.info('starting (auth_header=%r)' % self.auth_header)
    if not self.tempfile:
      self.tempfile = tempfile.NamedTemporaryFile(delete=True,
                                                  dir=self.download_dir)
    kwargs = dict(url=self.url,
                  request_timeout=3600.0,
                  streaming_callback=self.tempfile.write,
                  use_gzip=True, allow_ipv6=True,
                  user_agent='tr69-cpe-agent')
    if self.auth_header:
      kwargs.update(dict(headers=dict(Authorization=self.auth_header)))
    elif self.username and self.password:
      kwargs.update(dict(auth_username=self.username,
                         auth_password=self.password))
    req = tornado.httpclient.HTTPRequest(**kwargs)
    self.http_client = HTTPCLIENT(io_loop=self.ioloop)
    self.http_client.fetch(req, self._async_fetch_callback) 
Example #3
Source File: download.py    From honeything with GNU General Public License v3.0 6 votes vote down vote up
def cleanup(self):
    """Attempt to stop all activity and clean up resources.

    Returns:
      False - successfully stopped and cleaned up
      string - the reason download cannot be safely cancelled right now.
    """
    dlstate = self.stateobj.dlstate
    if dlstate == self.INSTALLING:
      return 'Download is currently installing to flash'
    if dlstate == self.REBOOTING:
      return 'Download has been installed, awaiting reboot'
    if self.wait_handle:
      self.ioloop.remove_timeout(self.wait_handle)
      self.wait_handle = None
    if self.download:
      self.download.close()
      self.download = None
    self.stateobj.Delete() 
Example #4
Source File: download.py    From honeything with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, stateobj, transfer_complete_cb,
               download_dir=None, ioloop=None):
    """Download object.

    Args:
      stateobj: a PersistentObject to store state across reboots.
        This class requires that command_key and url attributes be present.
      transfer_complete_cb: function to send a TransferComplete message.
      ioloop: Tornado ioloop. Unit tests can pass in a mock.
    """
    self.stateobj = self._restore_dlstate(stateobj)
    self.transfer_complete_cb = transfer_complete_cb
    self.download_dir = download_path
    self.ioloop = ioloop or tornado.ioloop.IOLoop.instance()
    self.download = None
    self.downloaded_fileobj = None
    self.downloaded_file = None
    self.wait_handle = None
    # the delay_seconds started when we received the RPC, even if we have
    # downloaded other files and rebooted since then.
    if not hasattr(self.stateobj, 'wait_start_time'):
      self.stateobj.Update(wait_start_time=time.time()) 
Example #5
Source File: app.py    From tornado-shortener with MIT License 6 votes vote down vote up
def main():
    """
    Main function to start the webserver application and listen on the specified port.
    """
    tornado.options.parse_command_line()
    application = Application(tornado.options.options.domain,
                              tornado.options.options.salt,
                              tornado.options.options.redis_namespace,
                              tornado.options.options.redis_host,
                              int(tornado.options.options.redis_port),
                              tornado.options.options.redis_db,
                              tornado.options.options.redis_password,
                              int(tornado.options.options.ttl))
    if tornado.options.options.localhostonly:
        address = '127.0.0.1'
        logging.info('Listening to localhost only')
    else:
        address = ''
        logging.info('Listening to all addresses on all interfaces')
    application.listen(tornado.options.options.port, address=address, xheaders=True)
    tornado.ioloop.IOLoop.instance().start()


# Run main method if script is run from command line. 
Example #6
Source File: backend_webagg.py    From neural-network-animation with MIT License 6 votes vote down vote up
def start(cls):
        if cls.started:
            return

        # Set the flag to True *before* blocking on IOLoop.instance().start()
        cls.started = True

        """
        IOLoop.running() was removed as of Tornado 2.4; see for example
        https://groups.google.com/forum/#!topic/python-tornado/QLMzkpQBGOY
        Thus there is no correct way to check if the loop has already been
        launched. We may end up with two concurrently running loops in that
        unlucky case with all the expected consequences.
        """
        print("Press Ctrl+C to stop WebAgg server")
        sys.stdout.flush()
        try:
            tornado.ioloop.IOLoop.instance().start()
        except KeyboardInterrupt:
            print("Server is stopped")
            sys.stdout.flush()
        finally:
            cls.started = False 
Example #7
Source File: cpe_management_server.py    From honeything with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, platform_config, port, ping_path,
               acs_url=None, get_parameter_key=None,
               start_periodic_session=None, ioloop=None,
               restrict_acs_hosts=None):
    self.ioloop = ioloop or tornado.ioloop.IOLoop.instance()
    self.restrict_acs_hosts = restrict_acs_hosts
    self.ValidateAcsUrl(acs_url)
    if platform_config:
      self.ValidateAcsUrl(platform_config.GetAcsUrl())
    self.acs_url = acs_url
    self.platform_config = platform_config
    self.port = port
    self.ping_path = ping_path
    self.get_parameter_key = get_parameter_key
    self.start_periodic_session = start_periodic_session
    self.my_ip = None
    self._periodic_callback = None
    self._start_periodic_timeout = None
    self.config_copy = None

    self.config = ServerParameters()
    self.ConfigurePeriodicInform() 
Example #8
Source File: http.py    From honeything with GNU General Public License v3.0 6 votes vote down vote up
def _NewPingSession(self):
    if self.session:
      # $SPEC3 3.2.2 initiate at most one new session after this one closes.
      self.session.ping_received = True
      return

    # Rate limit how often new sessions can be started with ping to
    # once a minute
    current_time = helpers.monotime()
    elapsed_time = current_time - self.previous_ping_time
    allow_ping = (elapsed_time < 0 or
                  elapsed_time > self.rate_limit_seconds)
    if allow_ping:
      self.ping_timeout_pending = None
      self.previous_ping_time = current_time
      self._NewSession('6 CONNECTION REQUEST')
    elif not self.ping_timeout_pending:
      # Queue up a new session via tornado.
      callback_time = self.rate_limit_seconds - elapsed_time
      if callback_time < 1:
        callback_time = 1
      self.ping_timeout_pending = self.ioloop.add_timeout(
          datetime.timedelta(seconds=callback_time),
          self._NewTimeoutPingSession) 
Example #9
Source File: device_info.py    From honeything with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, device_id, ioloop=None):
    super(DeviceInfo181Linux26, self).__init__()
    assert isinstance(device_id, DeviceIdMeta)
    self.ioloop = ioloop or tornado.ioloop.IOLoop.instance()
    self._device_id = device_id
    self.MemoryStatus = MemoryStatusLinux26()
    self.ProcessStatus = ProcessStatusLinux26(ioloop=ioloop)
    self.Unexport('FirstUseDate')
    self.Unexport(lists='Location')
    self.Unexport(objects='NetworkProperties')
    self.Unexport('ProvisioningCode')
    self.Unexport(objects='ProxierInfo')
    self.TemperatureStatus = temperature.TemperatureStatus()
    self.VendorLogFileList = {}
    self.VendorConfigFileList = {}
    self.SupportedDataModelList = {}
    self.ProcessorList = {}
    self.X_CATAWAMPUS_ORG_LedStatusList = {}
    self._next_led_number = 1 
Example #10
Source File: backend_webagg.py    From ImageFusion with MIT License 6 votes vote down vote up
def start(cls):
        if cls.started:
            return

        # Set the flag to True *before* blocking on IOLoop.instance().start()
        cls.started = True

        """
        IOLoop.running() was removed as of Tornado 2.4; see for example
        https://groups.google.com/forum/#!topic/python-tornado/QLMzkpQBGOY
        Thus there is no correct way to check if the loop has already been
        launched. We may end up with two concurrently running loops in that
        unlucky case with all the expected consequences.
        """
        print("Press Ctrl+C to stop WebAgg server")
        sys.stdout.flush()
        try:
            tornado.ioloop.IOLoop.instance().start()
        except KeyboardInterrupt:
            print("Server is stopped")
            sys.stdout.flush()
        finally:
            cls.started = False 
Example #11
Source File: web.py    From viewfinder with Apache License 2.0 6 votes vote down vote up
def _when_complete(self, result, callback):
        try:
            if result is None:
                callback()
            elif isinstance(result, Future):
                if result.done():
                    if result.result() is not None:
                        raise ValueError('Expected None, got %r' % result.result())
                    callback()
                else:
                    # Delayed import of IOLoop because it's not available
                    # on app engine
                    from tornado.ioloop import IOLoop
                    IOLoop.current().add_future(
                        result, functools.partial(self._when_complete,
                                                  callback=callback))
            else:
                raise ValueError("Expected Future or None, got %r" % result)
        except Exception as e:
            self._handle_request_exception(e) 
Example #12
Source File: web.py    From viewfinder with Apache License 2.0 6 votes vote down vote up
def _when_complete(self, result, callback):
        try:
            if result is None:
                callback()
            elif isinstance(result, Future):
                if result.done():
                    if result.result() is not None:
                        raise ValueError('Expected None, got %r' % result.result())
                    callback()
                else:
                    # Delayed import of IOLoop because it's not available
                    # on app engine
                    from tornado.ioloop import IOLoop
                    IOLoop.current().add_future(
                        result, functools.partial(self._when_complete,
                                                  callback=callback))
            else:
                raise ValueError("Expected Future or None, got %r" % result)
        except Exception as e:
            self._handle_request_exception(e) 
Example #13
Source File: zmqserver.py    From meshcat-python with MIT License 6 votes vote down vote up
def __init__(self, zmq_url=None, host="127.0.0.1", port=None):
        self.host = host
        self.websocket_pool = set()
        self.app = self.make_app()
        self.ioloop = tornado.ioloop.IOLoop.current()

        if zmq_url is None:
            def f(port):
                return self.setup_zmq("{:s}://{:s}:{:d}".format(DEFAULT_ZMQ_METHOD, self.host, port))
            (self.zmq_socket, self.zmq_stream, self.zmq_url), _ = find_available_port(f, DEFAULT_ZMQ_PORT)
        else:
            self.zmq_socket, self.zmq_stream, self.zmq_url = self.setup_zmq(zmq_url)

        if port is None:
            _, self.fileserver_port = find_available_port(self.app.listen, DEFAULT_FILESERVER_PORT)
        else:
            self.app.listen(port)
            self.fileserver_port = port
        self.web_url = "http://{host}:{port}/static/".format(host=self.host, port=self.fileserver_port)

        self.tree = SceneTree() 
Example #14
Source File: backend_webagg.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def run(self):
        tornado.ioloop.IOLoop.instance().start() 
Example #15
Source File: twisted.py    From honeything with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        # always use a new ioloop
        super(_TestReactor, self).__init__(IOLoop()) 
Example #16
Source File: backend_webagg.py    From CogAlg with MIT License 5 votes vote down vote up
def start(cls):
        if cls.started:
            return

        """
        IOLoop.running() was removed as of Tornado 2.4; see for example
        https://groups.google.com/forum/#!topic/python-tornado/QLMzkpQBGOY
        Thus there is no correct way to check if the loop has already been
        launched. We may end up with two concurrently running loops in that
        unlucky case with all the expected consequences.
        """
        ioloop = tornado.ioloop.IOLoop.instance()

        def shutdown():
            ioloop.stop()
            print("Server is stopped")
            sys.stdout.flush()
            cls.started = False

        @contextmanager
        def catch_sigint():
            old_handler = signal.signal(
                signal.SIGINT,
                lambda sig, frame: ioloop.add_callback_from_signal(shutdown))
            try:
                yield
            finally:
                signal.signal(signal.SIGINT, old_handler)

        # Set the flag to True *before* blocking on ioloop.start()
        cls.started = True

        print("Press Ctrl+C to stop WebAgg server")
        sys.stdout.flush()
        with catch_sigint():
            ioloop.start() 
Example #17
Source File: test_requests.py    From opentracing-python-instrumentation with MIT License 5 votes vote down vote up
def tornado_url(request, base_url, _unused_port):

    class Handler(tornado.web.RequestHandler):
        def get(self):
            self.write(self.request.headers['ot-tracer-traceid'])
            app.headers = self.request.headers

    app = tornado.web.Application([('/', Handler)])

    def run_http_server():
        if asyncio_available:
            # In python 3+ we should make ioloop in new thread explicitly.
            asyncio.set_event_loop(asyncio.new_event_loop())
        io_loop = tornado.ioloop.IOLoop.current()
        http_server = tornado.httpserver.HTTPServer(app)
        http_server.add_socket(_unused_port[0])

        def stop():
            http_server.stop()
            io_loop.add_callback(io_loop.stop)
            thread.join()

        # finalizer should be added before starting of the IO loop
        request.addfinalizer(stop)

        io_loop.start()

    # running an http server in a separate thread in purpose
    # to make it accessible for the requests from the current thread
    thread = threading.Thread(target=run_http_server)
    thread.start()

    return base_url + '/' 
Example #18
Source File: http_test.py    From honeything with GNU General Public License v3.0 5 votes vote down vote up
def testNewPingSession(self):
    cpe_machine = self.getCpe()
    cpe_machine.previous_ping_time = 0

    # Create mocks of ioloop, and stubout the time function.
    m = mox.Mox()
    ioloop_mock = m.CreateMock(tornado.ioloop.IOLoop)
    m.StubOutWithMock(cpe_machine, "_NewSession")
    StubOutMonotime(m)

    # First call to _NewSession should get the time and trigger a new session
    GetMonotime()().AndReturn(1000)
    cpe_machine._NewSession(mox.IsA(str))

    # Second call to _NewSession should queue a session
    GetMonotime()().AndReturn(1001)
    ioloop_mock.add_timeout(mox.IsA(datetime.timedelta),
                            mox.IgnoreArg()).AndReturn(1)

    # Third call should get the time and then not do anything
    # since a session is queued.
    GetMonotime()().AndReturn(1001)

    # And the call to _NewTimeoutSession should call through to
    # NewPingSession, and start a new session
    GetMonotime()().AndReturn(1000 + cpe_machine.rate_limit_seconds)
    ioloop_mock.add_timeout(mox.IsA(datetime.timedelta),
                            mox.IgnoreArg()).AndReturn(2)
    cpe_machine.ioloop = ioloop_mock
    m.ReplayAll()

    # Real test starts here.
    cpe_machine._NewPingSession()
    cpe_machine._NewPingSession()
    cpe_machine._NewPingSession()
    cpe_machine._NewTimeoutPingSession()

    # Verify everything was called correctly.
    m.VerifyAll() 
Example #19
Source File: backend_webagg.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def start(cls):
        if cls.started:
            return

        """
        IOLoop.running() was removed as of Tornado 2.4; see for example
        https://groups.google.com/forum/#!topic/python-tornado/QLMzkpQBGOY
        Thus there is no correct way to check if the loop has already been
        launched. We may end up with two concurrently running loops in that
        unlucky case with all the expected consequences.
        """
        ioloop = tornado.ioloop.IOLoop.instance()

        def shutdown():
            ioloop.stop()
            print("Server is stopped")
            sys.stdout.flush()
            cls.started = False

        @contextmanager
        def catch_sigint():
            old_handler = signal.signal(
                signal.SIGINT,
                lambda sig, frame: ioloop.add_callback_from_signal(shutdown))
            try:
                yield
            finally:
                signal.signal(signal.SIGINT, old_handler)

        # Set the flag to True *before* blocking on ioloop.start()
        cls.started = True

        print("Press Ctrl+C to stop WebAgg server")
        sys.stdout.flush()
        with catch_sigint():
            ioloop.start() 
Example #20
Source File: backend_webagg.py    From CogAlg with MIT License 5 votes vote down vote up
def run(self):
        tornado.ioloop.IOLoop.instance().start() 
Example #21
Source File: pool.py    From tornadis with MIT License 5 votes vote down vote up
def __init__(self, max_size=-1, client_timeout=-1, autoclose=False,
                 **client_kwargs):
        """Constructor.

        Args:
            max_size (int): max size of the pool (-1 means "no limit").
            client_timeout (int): timeout in seconds of a connection released
                to the pool (-1 means "no timeout").
            autoclose (boolean): automatically disconnect released connections
                with lifetime > client_timeout (test made every
                client_timeout/10 seconds).
            client_kwargs (dict): Client constructor arguments.
        """
        self.max_size = max_size
        self.client_timeout = client_timeout
        self.client_kwargs = client_kwargs
        self.__ioloop = client_kwargs.get('ioloop',
                                          tornado.ioloop.IOLoop.instance())
        self.autoclose = autoclose
        self.__pool = deque()
        if self.max_size != -1:
            self.__sem = tornado.locks.Semaphore(self.max_size)
        else:
            self.__sem = None
        self.__autoclose_periodic = None
        if self.autoclose and self.client_timeout > 0:
            every = int(self.client_timeout) * 100
            if int(tornado.version[0]) >= 5:
                cb = tornado.ioloop.PeriodicCallback(self._autoclose,
                                                     every)
            else:
                cb = tornado.ioloop.PeriodicCallback(self._autoclose,
                                                     every, self.__ioloop)
            self.__autoclose_periodic = cb
            self.__autoclose_periodic.start() 
Example #22
Source File: http_download.py    From honeything with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, url, username=None, password=None,
               download_complete_cb=None, ioloop=None, download_dir=None):
    self.url = str(url)
    self.username = str(username)
    self.password = str(password)
    self.download_complete_cb = download_complete_cb
    self.ioloop = ioloop or tornado.ioloop.IOLoop.instance()
    self.download_dir = download_dir 
Example #23
Source File: http_test.py    From honeything with GNU General Public License v3.0 5 votes vote down vote up
def testNewPeriodicSessionPending(self):
    """Tests that no new periodic session starts if there is one pending."""
    cpe_machine = self.getCpe()

    # Create mocks of ioloop, and stubout the time function.
    m = mox.Mox()
    m.StubOutWithMock(cpe_machine, 'Run')
    cpe_machine.Run()
    m.ReplayAll()

    self.assertFalse(('2 PERIODIC', None) in cpe_machine.event_queue)
    cpe_machine.NewPeriodicSession()
    self.assertTrue(('2 PERIODIC', None) in cpe_machine.event_queue)
    cpe_machine.NewPeriodicSession()
    m.ReplayAll() 
Example #24
Source File: http_test.py    From honeything with GNU General Public License v3.0 5 votes vote down vote up
def testNewPeriodicSession(self):
    """Tests that _NewSession is called if the event queue is empty."""
    cpe_machine = self.getCpe()

    # Create mocks of ioloop, and stubout the time function.
    m = mox.Mox()
    m.StubOutWithMock(cpe_machine, '_NewSession')
    cpe_machine._NewSession('2 PERIODIC')
    m.ReplayAll()

    cpe_machine.NewPeriodicSession()
    m.VerifyAll() 
Example #25
Source File: http.py    From honeything with GNU General Public License v3.0 5 votes vote down vote up
def _ScheduleRetrySession(self, wait=None):
    """Start a timer to retry a CWMP session.

    Args:
      wait: Number of seconds to wait. If wait=None, choose a random wait
        time according to $SPEC3 section 3.2.1
    """
    if self.session:
      self.session.close()
      self.session = None
    if wait is None:
      self.retry_count += 1
      wait = self.cpe_management_server.SessionRetryWait(self.retry_count)
    self.start_session_timeout = self.ioloop.add_timeout(
        datetime.timedelta(seconds=wait), self._SessionWaitTimer) 
Example #26
Source File: cwmp_session.py    From honeything with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, acs_url, ioloop=None):
    self.http = HTTPCLIENT(max_simultaneous_connections=1,
                           io_loop=ioloop or tornado.ioloop.IOLoop.instance())
    self.acs_url = acs_url
    self.cookies = None
    self.my_ip = None
    self.my_port = None
    self.ping_received = False
    self.state = self.CONNECT 
Example #27
Source File: http.py    From honeything with GNU General Public License v3.0 5 votes vote down vote up
def Listen(ip, port, ping_path, acs, cpe, cpe_listener, platform_config,
           acs_url=None, ping_ip6dev=None, fetch_args=dict(), ioloop=None,
           restrict_acs_hosts=None):
  if not ping_path:
    ping_path = '/ping/%x' % random.getrandbits(120)
  while ping_path.startswith('/'):
    ping_path = ping_path[1:]
  cpe_machine = CPEStateMachine(ip=ip, cpe=cpe, listenport=port,
                                platform_config=platform_config,
                                ping_path=ping_path,
                                restrict_acs_hosts=restrict_acs_hosts,
                                acs_url=acs_url, ping_ip6dev=ping_ip6dev,
                                fetch_args=fetch_args, ioloop=ioloop)
  cpe.setCallbacks(cpe_machine.SendTransferComplete,
                   cpe_machine.TransferCompleteReceived,
                   cpe_machine.InformResponseReceived)
  handlers = []
  if acs:
    acshandler = api_soap.ACS(acs).Handle
    handlers.append(('/acs', Handler, dict(soap_handler=acshandler)))
    #print 'TR-069 ACS at http://*:%d/acs' % port
    ht.logger.info('TR-069 ACS at http://*:%d/acs' % port)
  if cpe and cpe_listener:
    cpehandler = cpe_machine.cpe_soap.Handle
    handlers.append(('/cpe', Handler, dict(soap_handler=cpehandler)))
    #print 'TR-069 CPE at http://*:%d/cpe' % port
    ht.logger.info('TR-069 CPE at http://*:%d/cpe' % port)
  if ping_path:
    handlers.append(('/' + ping_path, PingHandler,
                     dict(cpe_ms=cpe_machine.cpe_management_server,
                          callback=cpe_machine.PingReceived)))
    #print 'TR-069 callback at http://*:%d/%s' % (port, ping_path)
    ht.logger.info('TR-069 callback at http://*:%d/%s' % (port, ping_path))
  webapp = tornado.web.Application(handlers)
  webapp.listen(port)
  return cpe_machine 
Example #28
Source File: http.py    From honeything with GNU General Public License v3.0 5 votes vote down vote up
def _NewSession(self, reason):
    if not self.session:
      self._CancelSessionRetries()
      self.event_queue.appendleft((reason, None))
      self.session = cwmp_session.CwmpSession(
          acs_url=self.cpe_management_server.URL, ioloop=self.ioloop)
      self.Run() 
Example #29
Source File: http.py    From honeything with GNU General Public License v3.0 5 votes vote down vote up
def _CancelSessionRetries(self):
    """Cancel any pending CWMP session retry."""
    if self.start_session_timeout:
      self.ioloop.remove_timeout(self.start_session_timeout)
      self.start_session_timeout = None
    self.retry_count = 0 
Example #30
Source File: download.py    From honeything with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, ioloop=None):
    self.ioloop = ioloop or tornado.ioloop.IOLoop.instance()
    self._downloads = list()
    self._pending_complete = list()
    self.config_dir = download_path
    self.download_dir = download_path
    # Function to send RPCs, to be filled in by parent object.
    self.send_transfer_complete = None