def run(self): global serverStatus try: ioLoop = ioloop.IOLoop() ioLoop.make_current() webApp = web.Application([ (r"/", MainHandler), (r"/ws", WsHandler), (r"/assets/(.*)", web.StaticFileHandler, { "path": self.__websitePath + "/assets" }), (r"/images/(.*)", web.StaticFileHandler, { "path": self.__websitePath + "/images" }) ]) webApp.listen(port=self.__tornadoPort, address=self.__tornadoAddress) except Exception as reason: self.__logger.critical("Unable to create Tornado application") self.__logger.info("Reason: " + str(reason)) self.stop() self.__syncEvent.set() if self.__running == True: self.__syncEvent.set() try: self.__ioLoop = ioloop.IOLoop.instance() serverStatus = True self.__ioLoop.start() except Exception as reason: self.__logger.critical("Unable to create Tornado IO Loop") self.__logger.info("Reason: " + str(reason)) self.__logger.info("Tornado server closed") return
def monitor_data(args): global monitor_ioloop name = threading.current_thread().name log.info("Launching loop in thread %s", name) io_loop = ioloop.IOLoop() io_loop.make_current() monitor_ioloop = io_loop @gen.coroutine def monitor(): client = Zoonado(args.servers, chroot=args.chroot) yield client.start() def data_callback(new_data): log.info("Shared data set to '%s'", new_data) watcher = client.recipes.DataWatcher() watcher.add_callback("/shared-znode", data_callback) yield gen.moment io_loop.add_callback(monitor) io_loop.start()
class UserReminder(BaseHandler): _ioloop = ioloop.IOLoop().instance() @web.asynchronous @gen.coroutine def get(self): send_res = [] reservations = yield get_reservations() past_email = pickle.load(open('./data/emails.pkl', 'rb')) for reservation in reservations: show_id = reservation['showtime_id'] show_meta = yield get_showtime(show_id) date_str = show_meta['date_str'] user_id = reservation['user_id'] user = yield get_user(user_id) name = user['name'] email = user['email'] if email in past_email: continue try: yield send_reminder(email, name, date_str) send_res.append(email) except Exception as e: print("Exception while sending out emails: {0}".format(e)) os.makedirs("./data/", exist_ok=True) yield gen.sleep(10) send_res.extend(past_email) with open('./data/emails.pkl', 'wb+') as fd: pickle.dump(send_res, fd) return self.api_response({'reminder_status': "all sent!"})
def setup(self): LOG.info("Python Platform %s", platform.python_implementation()) LOG.info("libzmq version %s", zmq.zmq_version()) LOG.info("pyzmq version %s", zmq.__version__) LOG.info("tornado version %s", version) self.context = zmq.Context() self.loop = ioloop.IOLoop()
def handle(self, *args, **kwargs): # Initialize pubsub helper. pubsub = PubSub() if DIRECTOR_ENABLED: logger.info('Starting director.') pubsub.init_director() if FORWARDER_ENABLED: logger.info('Starting forwarder.') pubsub.init_forwarder() # Get factories for connection and tornado webapp. authenticator_factory = import_by_path(AUTHENTICATOR_FACTORY) connection_factory = import_by_path(CONNECTION_FACTORY) webapp_factory = import_by_path(WEBAPP_FACTORY) # Create app and listen on SEVER_PORT app = webapp_factory( connection_factory(authenticator_factory(), pubsub)) app.listen(SERVER_PORT) loop = ioloop.IOLoop().instance() try: logger.info('Starting omnibusd.') loop.start() except KeyboardInterrupt: logger.info('Received KeyboardInterrup, stopping omnibusd.') loop.stop()
class ShowtimeAccessTokens(BaseHandler): _ioloop = ioloop.IOLoop().instance() @web.asynchronous @gen.coroutine def get(self): showid = self.get_argument('showid') shares = self.get_arguments('share') passphrase = cryptohelper.recover_passphrase(shares) privkey_show = yield get_show_privatekey(showid, passphrase) result = { 'showid': showid, 'users': [], } users = yield get_user_keypair_from_showid(showid) for user in users: user_id = user['id'] user_privkey_pem = cryptohelper.decrypt_blob( privkey_show, user['enc_private_key']) cur_result = { 'id': user_id, 'publickey': user['public_key'], } user_privkey = cryptohelper.import_key(user_privkey_pem) access_tokens = yield get_user_tokens(user_id) for key, value in access_tokens.items(): if not isinstance(value, bytes): continue cur_result[key] = cryptohelper.decrypt_blob( user_privkey, value) result['users'].append(cur_result) return self.api_response(result)
def build_response(self, request, response): resp = HTTPResponse(request, response.status, headers=response.headers, effective_url=request.url, error=None, buffer="") resp._body = response.data f = Future() f.content = None if response.status < 200 or response.status >= 300: resp.error = HTTPError(response.status, response=resp) ioloop.IOLoop().current().add_callback(f.set_exception, resp.error) else: ioloop.IOLoop().current().add_callback(f.set_result, resp) return f
class PromotionKeysHandler(BaseHandler): _ioloop = ioloop.IOLoop().instance() @web.asynchronous @gen.coroutine def post(self): showtime_id = self.get_argument('showtime_id', None) count = int(self.get_argument('count', 1)) if showtime_id is None: return self.error(400, "Must provide 'showtime_id' to proceed.") # Verify that such a show exists. showtime = yield get_showtime(showtime_id) if showtime is None: return self.error(404, "Show time not found.") promotion_keys = yield [ create_promotion_key(showtime_id) for _ in range(count) ] self.api_response({'promotion_keys': promotion_keys}, 201) @web.asynchronous @gen.coroutine def get(self): promotion_keys = yield get_promotion_keys() self.api_response(promotion_keys)
def start(): # Create our own IOLoop, we're in another process io_loop = ioloop.IOLoop() io_loop.make_current() pytest_engine = PyTestEngine(__opts__, io_loop) # pylint: disable=undefined-variable io_loop.add_callback(pytest_engine.start) io_loop.start()
def poll_thread(self): """Receive messages from comm socket.""" if not PY2: # Create an event loop for the handlers. ioloop.IOLoop().initialize() while not self.comm_thread_close.is_set(): self.poll_one()
def __init__(self, connection, worker_id, payload_handler): Process.__init__(self) self._io_loop = ioloop.IOLoop() self._payload_handler = payload_handler self._ipc_channel = IpcChannel(connection, worker_id, self._inbound_callback, None, self.stop, self._io_loop) self._worker_id = worker_id
def run(self): self.io_loop = ioloop.IOLoop() application = web.Application([(r"/", DummyHandler), (r"/api/testing", DummyHandler)]) self.http_server = httpserver.HTTPServer(application) self.http_server.listen(5000) print("Start") self.io_loop.start()
def synchronous_coroutine(*args, **kwargs): async = lambda: coroutine(*args, **kwargs) # Like synchronous HTTPClient, create separate IOLoop for sync code io_loop = ioloop.IOLoop(make_current=False) try: return io_loop.run_sync(async) finally: io_loop.close()
def get_common_scheduler_streams( mon_addr, not_addr, reg_addr, config, logname, log_url, loglevel, in_thread, curve_serverkey, curve_publickey, curve_secretkey, ): if config: # unwrap dict back into Config config = Config(config) if in_thread: # use instance() to get the same Context/Loop as our parent ctx = zmq.Context.instance() loop = ioloop.IOLoop.current() else: # in a process, don't use instance() # for safety with multiprocessing ctx = zmq.Context() loop = ioloop.IOLoop(make_current=False) def connect(s, addr): return util.connect( s, addr, curve_serverkey=curve_serverkey, curve_secretkey=curve_secretkey, curve_publickey=curve_publickey, ) mons = zmqstream.ZMQStream(ctx.socket(zmq.PUB), loop) connect(mons, mon_addr) nots = zmqstream.ZMQStream(ctx.socket(zmq.SUB), loop) nots.setsockopt(zmq.SUBSCRIBE, b'') connect(nots, not_addr) querys = ZMQStream(ctx.socket(zmq.DEALER), loop) connect(querys, reg_addr) # setup logging. if in_thread: log = traitlets.log.get_logger() else: if log_url: log = connect_logger(logname, ctx, log_url, root="scheduler", loglevel=loglevel) else: log = local_logger(logname, loglevel) return config, ctx, loop, mons, nots, querys, log
def test_brokenjson_get(): responses.add(responses.GET, 'https://api.sparkpost.com/api/v1/transmissions', status=200, content_type='application/json', body='{"results":') with pytest.raises(SparkPostAPIException): sp = SparkPost('fake-key') ioloop.IOLoop().run_sync(sp.transmission.list)
def test_success_send(): responses.add(responses.POST, 'https://api.sparkpost.com/api/v1/transmissions', status=200, content_type='application/json', body='{"results": "yay"}') sp = SparkPost('fake-key') results = ioloop.IOLoop().run_sync(sp.transmission.send) assert results == 'yay'
def run(self): io_loop = ioloop.IOLoop() io_loop.make_current() application = web.Application([ (r"/pubstatus", StatusHandler, dict(metachecker=self.metachecker)), ]) http_server = httpserver.HTTPServer(application) http_server.listen(STATUS_PORT) io_loop.start()
def test_success_list(): responses.add(responses.GET, 'https://api.sparkpost.com/api/v1/transmissions', status=200, content_type='application/json', body='{"results": []}') sp = SparkPost('fake-key') response = ioloop.IOLoop().run_sync(sp.transmission.list) assert response == []
def run(): thread_loop = ioloop.IOLoop() # need fresh IO loop for run_sync() try: res = thread_loop.run_sync(partial(func, *args, **kwargs), timeout=10) except: main_loop.add_callback(fut.set_exc_info, sys.exc_info()) else: main_loop.add_callback(fut.set_result, res)
def __init__(self, future, id): if not AsyncOutput._Init: AsyncOutput.Init() AsyncOutput._Init = True self.id = id self.resp = None self.ioloop = ioloop.IOLoop() self.future = future ioloop.IOLoop.current().add_future(future, self._callback)
def test_nocontent_get(): responses.add(responses.GET, 'https://api.sparkpost.com/api/v1/transmissions', status=204, content_type='application/json', body='') sp = SparkPost('fake-key') response = ioloop.IOLoop().run_sync(sp.transmission.list) assert response is True
def _start_io_loop(self): """Start IOLoop then set ready threading.Event.""" def mark_as_ready(): self._ready.set() if not self._io_loop: self._io_loop = ioloop.IOLoop() self._io_loop.add_callback(mark_as_ready) self._io_loop.start()
def wait(): """Wait for a watched file to change, then restart the process. Intended to be used at the end of scripts like unit test runners, to run the tests again after any source file changes (but see also the command-line interface in `main`) """ io_loop = ioloop.IOLoop() start(io_loop) io_loop.start()
def setUp(self): # don't use instance, since that would allow tests to pollute each # other's state self.ioloop = ioloop.IOLoop() # we don't expect exceptions from callbacks. def fail_immediate(callback): traceback.print_exception(*sys.exc_info()) raise Exception('Exception from ioloop callback!') self.ioloop.handle_callback_exception = fail_immediate self.states = []
def io_loop(request): """Fix tornado-5 compatibility in pytest_tornado io_loop""" io_loop = ioloop.IOLoop() io_loop.make_current() def _close(): io_loop.clear_current() io_loop.close(all_fds=True) request.addfinalizer(_close) return io_loop
def main(): # curl -v -H "Transfer-Encoding: chunked" --data-binary @somefile http://127.0.0.1:8080/x # ffmpeg -i x.mp4 -chunked_post 1 -method POST -f mjpeg http://127.0.0.1:8080/x handlers = [(r'/x', SampleChunkedHandler)] app = web.Application(handlers, debug=True) io = ioloop.IOLoop() server = HTTPServer(app) server.listen(8080) IOLoop.instance().start()
def test_fail_send(): responses.add(responses.POST, 'https://api.sparkpost.com/api/v1/transmissions', status=500, content_type='application/json', body=""" {"errors": [{"message": "You failed", "description": "More Info"}]} """) with pytest.raises(SparkPostAPIException): sp = SparkPost('fake-key') ioloop.IOLoop().run_sync(sp.transmission.send)
def io_loop(request): """Same as pytest-tornado.io_loop, but re-scoped to module-level""" io_loop = ioloop.IOLoop() io_loop.make_current() def _close(): io_loop.clear_current() io_loop.close(all_fds=True) request.addfinalizer(_close) return io_loop
def start(self): if self.loop: return self.loop = ioloop.IOLoop() print('starting client') self.thread = threading.Thread(target=self._client_loop) self.thread.daemon = True self.thread.start()
def __init__(self, requests, timeout, max_clients, time_len=60): assert requests self._io_loop = ioloop.IOLoop() self._client = httpclient.AsyncHTTPClient( self._io_loop, max_clients=max_clients) self.requests = requests self.timeout = timeout self.max_clients = max_clients self.time_len = time_len