def test_await_raises_exception(self): async def a(arg): raise RuntimeError('foo') with pytest.raises(RuntimeError) as exc: await_(a(42)) assert exc.type == RuntimeError assert str(exc.value) == 'foo'
def add_and_select_data(conn, data): row = await_( conn.fetchrow("insert into mytable(data) values ($1) returning id", data)) id_ = row[0] result = await_( conn.fetchrow("select data from mytable where id=($1)", id_)) return result[0]
def test_await_function_with_internal_loop(self): var = None async def a(arg): nonlocal var var = arg return arg ret = await_(a(42)) assert ret == 42 assert var == 42 bridge.stop() assert not bridge.running assert not asyncio.get_event_loop().is_running()
async def __call__(self, scope, receive, send): if scope["type"] != "http": raise ValueError("WSGI wrapper received a non-HTTP scope") self.scope = scope with SpooledTemporaryFile(max_size=65536) as body: # Alright, wait for the http.request messages while True: message = await receive() if message["type"] != "http.request": raise ValueError( "WSGI wrapper received a non-HTTP-request message") body.write(message.get("body", b"")) if not message.get("more_body"): break body.seek(0) # Wrap send so it can be called from the subthread self.sync_send = await_(send) # Call the WSGI app await self.run_wsgi_app(body)
async def delete_sensor(sensor_id: int = None): await_(delete_sensor_from_db(sensor_id)) return {"STATUS": "OK"}
def t(): for i in range(1000): await_(asyncio.sleep(random.random() / 1000))
def b(): nonlocal var await_(asyncio.sleep(0.01)) var += 2 return var
def b(arg): return await_(a(arg))
async def add_new_humidity(sensor_id: int, req: HumidityRequest): await_(add_humidity_to_db(req, sensor_id)) return {"STATUS": "OK"}
def b(arg): with pytest.raises(RuntimeError) as error: await_(a(arg)) assert type(error.value) == RuntimeError and \ str(error.value) == '42'
def main(): for i in range(10): await_(asyncio.sleep(random.random())) print(i)