def _on_connect(self, future: Future) -> None: try: future.result() except Exception as e: self._error(e) else: logging.debug( 'mjpg client for camera %(camera_id)s connected on port %(port)s' % { 'port': self._port, 'camera_id': self._camera_id }) if self._auth_mode == 'basic': logging.debug('mjpg client using basic authentication') auth_header = utils.build_basic_header(self._username, self._password) self.write( b'GET / HTTP/1.1\r\nAuthorization: %s\r\nConnection: close\r\n\r\n' % auth_header) elif self._auth_mode == 'digest': # in digest auth mode, the header is built upon receiving 401 self.write(b'GET / HTTP/1.1\r\n\r\n') else: # no authentication self.write(b'GET / HTTP/1.1\r\nConnection: close\r\n\r\n') self._seek_http()
def test_coroutine(): prop_name = Faker().pystr() exposed_thing.add_property(prop_name, prop_init_non_observable) observable_prop = exposed_thing.on_property_change(prop_name) future_next = Future() future_error = Future() def on_next(item): future_next.set_result(item) def on_error(err): future_error.set_exception(err) subscription = observable_prop.subscribe(on_next=on_next, on_error=on_error) yield exposed_thing.write_property(prop_name, Faker().pystr()) with pytest.raises(Exception): future_error.result() assert not future_next.done() subscription.dispose()
def error_callback(future: Future) -> None: try: future.result() except Exception as e: if not isinstance(e, quiet_exceptions): app_log.error("Exception in Future %r after timeout", future, exc_info=True)
def error_callback(future: Future) -> None: try: future.result() except Exception as e: if not isinstance(e, quiet_exceptions): app_log.error( "Exception in Future %r after timeout", future, exc_info=True )
class UploadToProvider(BaseHandler): ''' Upload the file associated with the given mobius id to all providers. ''' def initialize(self, loop): self._loop = loop self._request_dealer = SocketFactory.dealer_socket("/request/local", on_recv=self._process_result, transport=stream.INPROC, bind=False, loop=loop) self._request_future = None def _process_result(self, envelope, msgs): response = msgs[-1] self._request_future.set_result(response) @gen.coroutine def get(self): log.info("UploadToProvider handler get") self._request_future = Future() mobius_id = int(self.get_argument("mobius_id", default=0)) user_id = self.current_user.id params = JSONObject() params.mobius_id = mobius_id params.user_id = user_id request = Request(command=Command.UPLOAD.value, params=params.json_string) self._request_dealer.send(request) log.info("Lets wait here one second.") yield self._request_future log.info("One second should have passed.") response = self._request_future.result() state = response.state while state.state_id == UPLOADING: result = JSONObject(state.response) log.info("Progress: {}".format(result.progress)) self._request_future = Future() yield self._request_future response = self._request_future.result() state = response.state if state.state_id == ERROR: self.set_status(500) self.write(state.error) elif state.state_id == RESULT: self.set_status(200) self.write(state.response) else: self.set_status(500) self.write("Unexpected error occurred.") self._request_future = None
def _on_before_www_authenticate(self, future: Future) -> None: try: future.result() except Exception as e: self._error(e) else: if self._check_error(): return r_future = utils.cast_future(self.read_until(b'\r\n')) r_future.add_done_callback(self._on_www_authenticate)
def _on_before_content_length(self, future: Future): try: future.result() except Exception as e: self._error(e) else: if self._check_error(): return r_future = utils.cast_future(self.read_until(b'\r\n\r\n')) r_future.add_done_callback(self._on_content_length)
def test_future_set_result_unless_cancelled(self): fut = Future() # type: Future[int] future_set_result_unless_cancelled(fut, 42) self.assertEqual(fut.result(), 42) self.assertFalse(fut.cancelled()) fut = Future() fut.cancel() is_cancelled = fut.cancelled() future_set_result_unless_cancelled(fut, 42) self.assertEqual(fut.cancelled(), is_cancelled) if not is_cancelled: self.assertEqual(fut.result(), 42)
def test_future_set_result_unless_cancelled(self): fut = Future() future_set_result_unless_cancelled(fut, 42) self.assertEqual(fut.result(), 42) self.assertFalse(fut.cancelled()) fut = Future() fut.cancel() is_cancelled = fut.cancelled() future_set_result_unless_cancelled(fut, 42) self.assertEqual(fut.cancelled(), is_cancelled) if not is_cancelled: self.assertEqual(fut.result(), 42)
def test_coroutine(): td = ThingDescription.from_thing(exposed_thing.thing) event_name = next(six.iterkeys(td.events)) future_conn = Future() future_event = Future() payload = Faker().sentence() def on_next(ev): if not future_conn.done(): future_conn.set_result(True) return if ev.data == payload: future_event.set_result(True) subscription = subscribe_func(event_name, on_next) while not future_conn.done(): yield tornado.gen.sleep(0) exposed_thing.emit_event(event_name, Faker().sentence()) exposed_thing.emit_event(event_name, payload) yield future_event assert future_event.result() subscription.dispose()
def test_coroutine(): td = ThingDescription.from_thing(exposed_thing.thing) prop_name = next(six.iterkeys(td.properties)) future_conn = Future() future_change = Future() prop_value = Faker().sentence() def on_next(ev): if not future_conn.done(): future_conn.set_result(True) return if ev.data.value == prop_value: future_change.set_result(True) subscription = subscribe_func(prop_name, on_next) while not future_conn.done(): yield tornado.gen.sleep(0) yield exposed_thing.write_property(prop_name, Faker().sentence()) yield exposed_thing.write_property(prop_name, prop_value) yield future_change assert future_change.result() subscription.dispose()
def test_notify_waiter(self): brewhouse = Brewhouse.objects.create(name="Foo") recipe = Recipe.objects.create(name="Bar") instance = RecipeInstance.objects.create(recipe=recipe) future = Future() recipe_instance.RecipeInstanceHandler.waiters[brewhouse.pk] = [future] recipe_instance.RecipeInstanceHandler.notify(brewhouse.pk, instance.pk) self.assertEquals(future.result(), {'recipe_instance': instance.pk})
def test_notify_waiter(self): brewhouse = Brewhouse.objects.create(name="Foo") recipe = Recipe.objects.create(name="Bar") instance = RecipeInstance.objects.create(recipe=recipe) future = Future() recipe_instance.RecipeInstanceHandler.waiters[brewhouse.pk] = [future] recipe_instance.RecipeInstanceHandler.notify(brewhouse.pk, instance.pk) self.assertEquals(future.result(), {'recipe_instance': instance.pk})
def read_input_registers(self, address, count=1, **kwargs): fut_result = Future() request = ReadInputRegistersRequest(address, count, **kwargs) yield self.sem.acquire() try: res = self.execute(request) res.addCallback(fut_result.set_result) yield fut_result finally: self.sem.release() raise gen.Return(fut_result.result())
def _on_http(self, future: Future) -> None: try: data = future.result() except Exception as e: self._error(e) else: if data.endswith(b'401 '): self._seek_www_authenticate() else: # no authorization required, skip to content length self._seek_content_length()
def test_finish(self): f = Future() ag = AsyncGroup(partial(f.set_result, True)) self.assertEqual(ag._finished, False) ag.add_notification() ag.finish() self.assertEqual(ag._finished, True) self.assertEqual(f.result(), True)
def write_register(self, address, value, **kwargs): fut_result = Future() request = WriteSingleRegisterRequest(address, value, **kwargs) yield self.sem.acquire() try: res = self.execute(request) res.addCallback(fut_result.set_result) yield fut_result finally: self.sem.release() raise gen.Return(fut_result.result())
def test_finish(self): f = Future() ag = AsyncGroup(partial(f.set_result, True)) self.assertEqual(ag._finished, False) ag.add_notification() ag.finish() self.assertEqual(ag._finished, True) self.assertEqual(f.result(), True)
def generate_challenge(self): """Returns authentication challenge message, with OAuth URL.""" nonce = uuid.uuid4().hex future = Future() future.add_done_callback(lambda future: self.session.transport.write_json( self.authenticate(future.result()))) self.session.properties.futures[nonce] = future return { "url": "http://robust.brendan.so/auth/twitter?robust_token=%s" % nonce }
def test_notify_waiter_only_correct_brewhouse(self): brewhouse1 = Brewhouse.objects.create(name="Foo") brewhouse2 = Brewhouse.objects.create(name="Baz") recipe = Recipe.objects.create(name="Bar") instance = RecipeInstance.objects.create(recipe=recipe) future1 = Future() future2 = Future() recipe_instance.RecipeInstanceHandler.waiters[brewhouse1.pk] = [future1] recipe_instance.RecipeInstanceHandler.waiters[brewhouse2.pk] = [future2] recipe_instance.RecipeInstanceHandler.notify(brewhouse1.pk, instance.pk) self.assertEquals(future1.result(), {'recipe_instance': instance.pk}) self.assertFalse(future2.done())
class QuoteHandler(BaseHandler): ''' Remove me. ''' def initialize(self, loop): self._loop = loop self._request_dealer = SocketFactory.dealer_socket("/request/local", on_recv=self._process_result, transport=stream.INPROC, bind=False, loop=loop) self._request_future = None def _process_result(self, envelope, msgs): response = msgs[-1] self._request_future.set_result(response) @authenticated @gen.coroutine def get(self): log.info("Test handler get") self._request_future = Future() mobius_id = int(self.get_argument("mobius_id", default=1)) user_id = self.current_user.id params = JSONObject() params.mobius_id = mobius_id params.user_id = user_id params.http_params = {Parameter.QUANTITY.name: 1, Parameter.SCALE.name: 0.1, Parameter.UNIT.name: "cm"} # Parameter.MATERIAL.name: "metal_cast_silver_sanded"} request = Request(command=Command.QUOTE.value, params=params.json_string) self._request_dealer.send(request) log.info("Lets wait here one second.") yield self._request_future log.info("One second should have passed.") response = self._request_future.result() state = response.state if state.state_id == ERROR: self.set_status(500) self.write(state.error) elif state.state_id == RESULT: self.set_status(200) self.write(state.response) else: self.set_status(500) self.write("Unexpected error occurred.") self._request_future = None
def _on_jpg(self, future: Future): try: data = future.result() except Exception as e: self._error(e) else: self._last_jpg = data self._last_jpg_times.append(time.time()) while len(self._last_jpg_times) > self._FPS_LEN: self._last_jpg_times.pop(0) self._seek_content_length()
def wrapper(*args, **kwargs): future = Future() if replace_callback and 'callback' in kwargs: callback = kwargs.pop('callback') IOLoop.current().add_future( future, lambda future: callback(future.result())) try: result = func(*args, **kwargs) except (Return, StopIteration) as e: result = _value_from_stopiteration(e) except Exception: future_set_exc_info(future, sys.exc_info()) return future else: if isinstance(result, GeneratorType): # Inline the first iteration of Runner.run. This lets us # avoid the cost of creating a Runner when the coroutine # never actually yields, which in turn allows us to # use "optional" coroutines in critical path code without # performance penalty for the synchronous case. try: orig_stack_contexts = stack_context._state.contexts yielded = next(result) if stack_context._state.contexts is not orig_stack_contexts: yielded = Future() yielded.set_exception( stack_context.StackContextInconsistentError( 'stack_context inconsistency (probably caused ' 'by yield within a "with StackContext" block)')) except (StopIteration, Return) as e: future.set_result(_value_from_stopiteration(e)) except Exception: future_set_exc_info(future, sys.exc_info()) else: _futures_to_runners[future] = Runner(result, future, yielded) yielded = None try: return future finally: # Subtle memory optimization: if next() raised an exception, # the future's exc_info contains a traceback which # includes this stack frame. This creates a cycle, # which will be collected at the next full GC but has # been shown to greatly increase memory usage of # benchmarks (relative to the refcount-based scheme # used in the absence of cycles). We can avoid the # cycle by clearing the local variable after we return it. future = None future.set_result(result) return future
def test_await_future(self): f = Future() tf = TornadoFuture() def finish_later(): time.sleep(0.1) f.set_result('future') tf.set_result('tornado') Thread(target=finish_later).start() assert self.client.wait([f, tf]) assert f.done() assert tf.done() assert f.result() == 'future' assert tf.result() == 'tornado'
def test_await_future(self): f = Future() tf = TornadoFuture() def finish_later(): time.sleep(0.1) f.set_result('future') tf.set_result('tornado') Thread(target=finish_later).start() assert self.client.wait([f, tf]) assert f.done() assert tf.done() assert f.result() == 'future' assert tf.result() == 'tornado'
def generate_challenge(self): """Returns authentication challenge message, with OAuth URL.""" nonce = uuid.uuid4().hex future = Future() future.add_done_callback( lambda future: self.session.transport.write_json( self.authenticate(future.result()))) self.session.properties.futures[nonce] = future return { "url": "http://robust.brendan.so/auth/twitter?robust_token=%s" % nonce }
def _on_www_authenticate(self, future: Future) -> None: try: data = future.result() except Exception as e: self._error(e) else: if self._check_error(): return data = data.strip() m = re.match(b'Basic\s*realm="([a-zA-Z0-9\-\s]+)"', data) if m: logging.debug('mjpg client using basic authentication') auth_header = utils.build_basic_header(self._username, self._password) w_data = b'GET / HTTP/1.1\r\nAuthorization: %s\r\nConnection: close\r\n\r\n' % auth_header w_future = utils.cast_future(self.write(w_data)) w_future.add_done_callback(self._seek_http) return if data.startswith('Digest'): logging.debug('mjpg client using digest authentication') parts = data[7:].split(',') parts_dict = dict(p.split('=', 1) for p in parts) parts_dict = { p[0]: p[1].strip('"') for p in list(parts_dict.items()) } self._auth_digest_state = parts_dict auth_header = utils.build_digest_header( 'GET', '/', self._username, self._password, self._auth_digest_state) w_data = b'GET / HTTP/1.1\r\nAuthorization: %s\r\nConnection: close\r\n\r\n' % auth_header w_future = utils.cast_future(self.write(w_data)) w_future.add_done_callback(self._seek_http) return logging.error('mjpg client unknown authentication header: "%s"' % data) self._seek_content_length()
def test_notifications(self): f = Future() ag = AsyncGroup(partial(f.set_result, True)) not1 = ag.add_notification() not2 = ag.add_notification() self.assertEqual(ag._finish_cb_called, False) not1() self.assertEqual(ag._finish_cb_called, False) not2('params', are='ignored') self.assertEqual(ag._finish_cb_called, True) self.assertEqual(ag._aborted, False) self.assertEqual(f.result(), True)
def test_notify_waiter_only_correct_brewhouse(self): brewhouse1 = Brewhouse.objects.create(name="Foo") brewhouse2 = Brewhouse.objects.create(name="Baz") recipe = Recipe.objects.create(name="Bar") instance = RecipeInstance.objects.create(recipe=recipe) future1 = Future() future2 = Future() recipe_instance.RecipeInstanceHandler.waiters[brewhouse1.pk] = [ future1 ] recipe_instance.RecipeInstanceHandler.waiters[brewhouse2.pk] = [ future2 ] recipe_instance.RecipeInstanceHandler.notify(brewhouse1.pk, instance.pk) self.assertEquals(future1.result(), {'recipe_instance': instance.pk}) self.assertFalse(future2.done())
def test_notifications(self): f = Future() ag = AsyncGroup(partial(f.set_result, True)) not1 = ag.add_notification() not2 = ag.add_notification() self.assertEqual(ag._finish_cb_called, False) not1() self.assertEqual(ag._finish_cb_called, False) not2('params', are='ignored') self.assertEqual(ag._finish_cb_called, True) self.assertEqual(ag._aborted, False) self.assertEqual(f.result(), True)
def make_request(message, socket): ''' Helper method to make async service requests. @param message - message to be sent to service @param socket - socket to send it on @returns service response message ''' future = Future() # set result callback socket.on_recv(lambda _, msgs: future.set_result(msgs[-1])) socket.send(message) yield future # clear result callback socket.on_recv(None) return future.result()
def test_notifications(self): f = Future() ag = AsyncGroup(partial(f.set_result, True)) not1 = ag.add_notification() not2 = ag.add_notification() self.assertEqual(ag._finished, False) not1() self.assertEqual(ag._finished, False) not2('params', are='ignored') self.assertEqual(ag._finished, True) self.assertEqual(f.result(), True) with ExpectLog(async_logger, r'.*trying to finish already finished AsyncGroup\(name=None, finished=True\)'): ag.finish()
def _on_content_length(self, future: Future): try: data = future.result() except Exception as e: self._error(e) else: if self._check_error(): return matches = re.findall(rb'(\d+)', data) if not matches: self._error( 'could not find content length in mjpg header line "%(header)s"' % {'header': data}) return length = int(matches[0]) r_future = utils.cast_future(self.read_bytes(length)) r_future.add_done_callback(self._on_jpg)
def test_notifications(self): f = Future() ag = AsyncGroup(partial(f.set_result, True)) not1 = ag.add_notification() not2 = ag.add_notification() self.assertEqual(ag._finished, False) not1() self.assertEqual(ag._finished, False) not2('params', are='ignored') self.assertEqual(ag._finished, True) self.assertEqual(f.result(), True) with ExpectLog( async_logger, r'.*trying to finish already finished AsyncGroup\(name=None, finished=True\)' ): ag.finish()
def _discard_future_result(self, future: Future) -> None: """Avoid unhandled-exception warnings from spawned coroutines.""" future.result()
def async_fetch_future(url): http_client = AsyncHTTPClient() my_future = Future() fetch_future = http_client.fetch(url) fetch_future.add_done_callback(lambda f: my_future.set_result(f.result())) return my_future.result()
def mock_on_response(future: Future) -> None: resp = future.result() self.stop() callback_result.append((resp.cameras, resp.error))
class StreamHandler(PostContentHandler): ''' Upload file request handler. It downloads the file in streaming mode from the client, and stores the downloaded files in the provided temporary directory. ''' DISPOSITION = "Content-Disposition" FILE_FIELD = "fileID" NAME_FIELD = "fileName" POST_SUCCESS = 201 def initialize(self, loop): ''' The initialization method for this request handler. @param loop - zmq eventloop ''' super(StreamHandler, self).initialize() self._tmp_file = tempfile.NamedTemporaryFile(dir=get_tmp_dir(), delete=False) self._upload_pub = SocketFactory.dealer_socket("/db/request", bind=False, on_recv=self._handle_response, loop=loop) self._cur_headers = None self._file_started = False self._user_file_name = None self._upload_future = None self._uploaded_model_id = None def get_current_user(self): ''' Fetch current users information. ''' user = escape.to_basestring(self.get_secure_cookie("mobius_user")) if user is None: return None return JSONObject(user) def _handle_response(self, envelope, msgs): ''' Handle the response from the database. @param envelope - a list of router socket ids @param msgs - list of messages ''' self._upload_future.set_result(msgs[-1]) def on_finish(self): ''' Do the clean ups here. ''' self._tmp_file.close() def post(self): ''' Overriden post method to respond to the client. ''' self.set_header("Content-Type", "text/plain") self.set_status(self.POST_SUCCESS) if self._uploaded_model_id is not None: self.write(json.dumps({"success": True, "model_id": self._uploaded_model_id})) else: self.write(json.dumps({"success": False, "model_id": -1})) self.finish() def _write_file_data(self, headers, data): ''' Writes the data to the temporary file. @param headers - HTTP headers for this file @param data - a chunk of file data ''' self._tmp_file.write(data) def _process_form_field(self, headers, data): ''' Saves the data entered into the upload form. @param headers - HTTP headers for this form field @param data - data entered into the field. ''' if self._get_field_name(headers) == self.NAME_FIELD: self._user_file_name = data.decode(self.HEADER_ENCODING) else: log.info("Field name: {}".format(self._get_field_name(headers))) def _get_field_name(self, headers): ''' Retrieve the name of the field from the given headers. @param headers - HTTP headers @returns name of the field specified on the page ''' try: return headers[self.DISPOSITION]["params"]["name"] except KeyError: return None def receive_data(self, headers, chunk): if self._cur_headers != headers: self._cur_headers = headers print("WTF") # Process different content types differently if self._get_field_name(self._cur_headers) == self.FILE_FIELD: self._write_file_data(self._cur_headers, chunk) else: self._process_form_field(self._cur_headers, chunk) receive_data.__doc__ = PostContentHandler.receive_data.__doc__ @tornado.gen.coroutine def request_done(self): self._upload_future = Future() params = JSONObject() params.path = self._tmp_file.name params.filename = self._user_file_name params.user_id = self.current_user.id upload_file = Request(command=Command.SAVE_FILE, params=params.json_string) log.info("Sending upload file: {0}".format(str(upload_file))) self._upload_pub.send(upload_file) yield self._upload_future try: response = self._upload_future.result() state = response.state if state.state_id == RESULT: log.debug("Successfully uploaded file {}".format(self._user_file_name)) json_resp = JSONObject(state.response) self._uploaded_model_id = json_resp.model_id elif state.state_id == ERROR: log.debug("Failed to upload file {}".format(self._user_file_name)) else: log.error("Unexpected response received: {}".format(str(response))) except: log.error("Error while uploading file: {}".format(self._user_file_name)) request_done.__doc__ = PostContentHandler.request_done.__doc__
def _discard_future_result(self, future: Future) -> None: """Avoid unhandled-exception warnings from spawned coroutines.""" future.result()
from tornado.concurrent import Future from concurrent.futures import ProcessPoolExecutor import time def work(x): time.sleep(4) return 'work is finished' def work_done(res): print('work done callback triggered ' + res) with ProcessPoolExecutor() as pool: my_future = Future() my_future = pool.submit(work, 'a') my_future.add_done_callback(work_done) print(my_future) print(my_future.result())