Example #1
0
def run(script_path):
    """Run a script in a separate thread and start a server for the report.

    This starts a blocking ioloop.

    Parameters
    ----------
    script_path : str

    """
    _fix_sys_path(script_path)

    # Install a signal handler that will shut down the ioloop
    # and close all our threads
    _set_up_signal_handler()

    # Schedule the server to start using the IO Loop on the main thread.
    server = Server(script_path,
                    sys.argv,
                    on_server_start_callback=_on_server_start)

    server.add_preheated_report_context()

    ioloop = tornado.ioloop.IOLoop.current()
    ioloop.spawn_callback(server.loop_coroutine)

    ioloop.start()
Example #2
0
 def new_reference(self, sensor, value):
     #print("I GOT A VALUE NICE")
     #print("Coming from sensor", str(sensor), id(sensor))
     #print("buffer = ", self.buffer)
     if type(value) is self.actual_graph.cls:
         # Add it to the buffer!
         assert sensor.SID in self.sensors
         if sensor.SID not in self.buffer:
             #print("New value!")
             self.buffer[sensor.SID] = [(value.value, value.time)]
         else:
             #print("Already has a value, but we'll add it anyways")
             self.buffer[sensor.SID].append((value.value, value.time))
         # Check if the buffer is full
         if len(self.buffer) == len(self.sensors):
             #print("Full buffer!")
             s = self.sum_and_clear_buffer()
             self.values.append(s)
             ioloop.spawn_callback(self.send_add, [s])
             # Clean up values
             start = now() + self.actual_graph.timespan_start
             todelete = []
             for v in self.values:
                 if v[1] < start:
                     todelete.append(v)
             if len(todelete) > 0:
                 for v in todelete:
                     self.values.remove(v)
                 #print("Removed {} values".format(len(todelete)))
                 ioloop.spawn_callback(self.send_delete, todelete)
Example #3
0
 def new_reference(self, sensor, value):
     #print("I GOT A VALUE NICE")
     #print("Coming from sensor", str(sensor), id(sensor))
     #print("buffer = ", self.buffer)
     if type(value) is self.actual_graph.cls:
         # Add it to the buffer!
         assert sensor.SID in self.sensors
         if sensor.SID not in self.buffer:
             #print("New value!")
             self.buffer[sensor.SID] = [(value.value, value.time)]
         else:
             #print("Already has a value, but we'll add it anyways")
             self.buffer[sensor.SID].append((value.value, value.time))
         # Check if the buffer is full
         if len(self.buffer) == len(self.sensors):
             #print("Full buffer!")
             s = self.sum_and_clear_buffer()
             self.values.append(s)
             ioloop.spawn_callback(self.send_add, [s])
             # Clean up values
             start = now() + self.actual_graph.timespan_start
             todelete = []
             for v in self.values:
                 if v[1] < start:
                     todelete.append(v)
             if len(todelete) > 0:
                 for v in todelete:
                     self.values.remove(v)
                 #print("Removed {} values".format(len(todelete)))
                 ioloop.spawn_callback(self.send_delete, todelete)
Example #4
0
def main():
    options.parse_command_line()
    from api.v1.urls import urls
    from tornado.web import Application
    import tornado.ioloop
    app = Application(urls, debug=options.debug)
    app.listen(options.port)
    ioloop = tornado.ioloop.IOLoop.instance()
    Cache.connect(endpoint_url='http://localhost:8000')
    Persis.connect()
    Rds.connect()
    ioloop.spawn_callback(sync_record_data)
    ioloop.start()
Example #5
0
def run(msg_queue):
    # TODO: use random port; http://stackoverflow.com/questions/9536531/bind-tornado-webserver-on-random-port
    application.listen(9999)
    ioloop = tornado.ioloop.IOLoop.instance()
    ioloop.make_current()
    ioloop.spawn_callback(process_outbound_messages, msg_queue)

    while True:
        try:
            ioloop.start()
        except KeyboardInterrupt:
            # This process should not be interrupted
            pass
def main():
    logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT)
    settings = {
        "static_path": os.path.join(os.path.dirname(__file__), "static",
                                    "static")
    }
    # routes = ROUTES
    client = motor.motor_tornado.MotorClient(args.mongo_url)
    LOGGER.info(f"Connected to MongoDB on {args.mongo_url}")
    application = tornado.web.Application(ROUTES,
                                          debug=True,
                                          serve_traceback=True,
                                          autoreload=True,
                                          **settings)
    application.mongo = client

    config = load_config_file()

    queue = Queue(maxsize=10)
    application.zmq_poller = ZMQPoller(queue)
    sampler = MongoDBSampler(config['servers'], client, queue)

    ioloop = tornado.ioloop.IOLoop.instance()
    ioloop.spawn_callback(application.zmq_poller.pulling)
    ioloop.spawn_callback(sampler.sample)

    # application.db = RiakDB(args.riak_url)
    # LOGGER.info('Riak connected')
    application.listen(args.port)
    LOGGER.info(f"Server is now at: 127.0.0.1:{args.port}")

    try:
        ioloop.start()
    except KeyboardInterrupt:
        pass
    finally:
        print("Closing server...\n")
        tornado.ioloop.IOLoop.instance().stop()
Example #7
0
def await (func, *args, **kwargs):
    s = threading.Semaphore(0)
    val = {"result": None}

    async def wait_for_result(_func=func,
                              _args=args,
                              _kwargs=kwargs,
                              _s=s,
                              _val=val):
        try:
            if isinstance(_func, types.CoroutineType):
                _val["result"] = await _func
            else:
                res = _func(*_args, **_kwargs)
                if isinstance(res, types.CoroutineType):
                    _val["result"] = await res
                else:
                    _val["result"] = res
        finally:
            _s.release()

    ioloop.spawn_callback(wait_for_result)
    s.acquire()
    return val["result"]
Example #8
0
def main():
    parse_command_line()

    application = tornado.web.Application([
        (r"/", MainHandler),
        (r"/health", HealthHandler),
        (r"/slow", SlowHandler),
    ])

    ioloop = tornado.ioloop.IOLoop.instance()
    application.listen(options.port)

    logger.debug("Starting %r on port:%d version:%r",
                 __file__,
                 options.port, options.version)

    if options.register_from or options.register_to:
        if not options.register_from and options.register_to:
            raise Exception(
                'If you specify --register_from or --register_to you have to specify both')

        ioloop.spawn_callback(register_self)

    ioloop.start()
Example #9
0
def main():
    parse_command_line()

    application = tornado.web.Application([
        (r"/", MainHandler),
        (r"/health", HealthHandler),
        (r"/slow", SlowHandler),
    ])

    ioloop = tornado.ioloop.IOLoop.instance()
    application.listen(options.port)

    logger.debug("Starting %r on port:%d version:%r", __file__, options.port,
                 options.version)

    if options.register_from or options.register_to:
        if not options.register_from and options.register_to:
            raise Exception(
                'If you specify --register_from or --register_to you have to specify both'
            )

        ioloop.spawn_callback(register_self)

    ioloop.start()
Example #10
0
            d[i] = user.__dict__
        self.write(json.dumps(d))

    def put(self):
        try:
            ## TODO: Data validation
            soc_runner = self.application.soc_runner
            d = json.loads(self.request.body.decode())
            soc_runner._users = list(
                map(
                    lambda x: UserData(user=x['_user'],
                                       email=x['_email'],
                                       courses=x['_courses']), d.values()))
            soc_runner.loop()
            print(soc_runner._users)
        except ValueError:
            self.write('ValueError!')


if __name__ == "__main__":
    app = Application()
    app.listen(8088)
    ioloop = tornado.ioloop.IOLoop.instance()

    def refresh():
        app.soc_runner.loop()
        ioloop.call_later(15, refresh)

    ioloop.spawn_callback(refresh)
    ioloop.start()
Example #11
0
@tornado.gen.coroutine
def print_response(url):
    response = yield fetch_coroutine(url)
    print(response)


def synchronous_fetch(url):
    http_client = tornado.httpclient.HTTPClient()
    response = http_client.fetch(url)
    return response.body


def asynchronous_fetch(url, callback):
    http_client = tornado.httpclient.AsyncHTTPClient()
    def handle_response(response):
        callback(response.body)
    http_client.fetch(url, callback=handle_response)


if __name__ == '__main__':
    ioloop = tornado.ioloop.IOLoop.current()
    print(synchronous_fetch('http://www.baidu.com/'))
    #  if the `.IOLoop` is not yet running, you can start the IOLoop, run the coroutine,
    # and then stop the IOLoop with the IOLoop.run_sync method.
    ioloop.run_sync(lambda: asynchronous_fetch('http://www.baidu.com', lambda x: print(x)))
    # call asynchronous func
    ioloop.spawn_callback(asynchronous_fetch, 'http://www.baidu.com', lambda x: print(x))
    ioloop.spawn_callback(print_response, 'http://www.baidu.com')
    ioloop.start()
Example #12
0
    return tornado.web.Application([
        # (r"(?P<url>.*)", PassThroughHandler),
        (r"/requests/status.json", VlcStatusHandler),
        (r"/requests/playlist.json", VlcPlaylistHandler),
        (r"/requests/browse.json", VlcBrowseHandler),
        (r"/.*", RootHandler),

        # vlc version unrecognized with this: :(
        # (r"/()", AuthStaticHandler,
        #  {"path": vlc_static_root, "default_filename": "index.html"})

        # pass-through for debugging
        # (r"/.*", PassThroughHandler),
    ]
    )


if __name__ == "__main__":
    app = make_app()
    app.listen(config.vlc_port)

    status_poller = StatusPoller()

    tornado.options.parse_command_line()
    ioloop = tornado.ioloop.IOLoop.instance()
    ioloop.spawn_callback(status_poller.run)
    try:
        ioloop.start()
    except KeyboardInterrupt:
        ioloop.stop()
Example #13
0
class UserDataStatusHandler(tornado.web.RequestHandler):  
    def get(self):
        soc_runner = self.application.soc_runner
        d = {}
        for i, user in enumerate(soc_runner._users, 0):
            d[i] = user.__dict__
        self.write(json.dumps(d))
    
    def put(self):
        try:
            ## TODO: Data validation
            soc_runner = self.application.soc_runner
            d = json.loads(self.request.body.decode())
            soc_runner._users = list(map(lambda x: UserData(user=x['_user'], email=x['_email'], courses=x['_courses']), d.values()))
            soc_runner.loop()
            print(soc_runner._users)
        except ValueError:
            self.write('ValueError!')


if __name__ == "__main__":
    app = Application()
    app.listen(8088)
    ioloop = tornado.ioloop.IOLoop.instance()
    def refresh():
        app.soc_runner.loop()
        ioloop.call_later(15, refresh)
    ioloop.spawn_callback(refresh)
    ioloop.start()
Example #14
0
    print(response)


def synchronous_fetch(url):
    http_client = tornado.httpclient.HTTPClient()
    response = http_client.fetch(url)
    return response.body


def asynchronous_fetch(url, callback):
    http_client = tornado.httpclient.AsyncHTTPClient()

    def handle_response(response):
        callback(response.body)

    http_client.fetch(url, callback=handle_response)


if __name__ == '__main__':
    ioloop = tornado.ioloop.IOLoop.current()
    print(synchronous_fetch('http://www.baidu.com/'))
    #  if the `.IOLoop` is not yet running, you can start the IOLoop, run the coroutine,
    # and then stop the IOLoop with the IOLoop.run_sync method.
    ioloop.run_sync(
        lambda: asynchronous_fetch('http://www.baidu.com', lambda x: print(x)))
    # call asynchronous func
    ioloop.spawn_callback(asynchronous_fetch, 'http://www.baidu.com',
                          lambda x: print(x))
    ioloop.spawn_callback(print_response, 'http://www.baidu.com')
    ioloop.start()
Example #15
0
 def cleanup(self):
     self.logger.info("Cleanup!")
     ioloop.spawn_callback(model.Value.clean, self.model.db)
Example #16
0
 def cleanup(self):
     self.logger.info("Cleanup!")
     ioloop.spawn_callback(model.Value.clean, self.model.db)