Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def test():
close_string = 'Super important close message.'
async with anyio.create_task_group() as task_group:
con = await create_websocket(task_group, 'wss://echo.websocket.org', use_ssl=True)
self.assertIsInstance(con, WebSocketConnection)
assert not con.closed
await con.close(1000, close_string)
assert con.closed
self.assertIsNone(con.subprotocol)
self.assertEqual(con.close_code.value, 1000)
self.assertEqual(con.close_reason, close_string)
async def testfunc():
async with create_task_group() as tg:
await tg.spawn(sleep, 0)
async def main():
async with purerpc.insecure_channel("localhost", port) as channel:
async with anyio.create_task_group() as task_group:
for _ in range(10):
await task_group.spawn(worker, channel)
async def main():
async with purerpc.insecure_channel("localhost", port) as channel:
async with anyio.create_task_group() as task_group:
for _ in range(50):
await task_group.spawn(worker, channel)
async def main():
async with purerpc.insecure_channel("localhost", port) as channel:
async with anyio.create_task_group() as task_group:
for _ in range(50):
await task_group.spawn(worker, channel)
if isinstance(data, dict):
data = encoder(data)
if isinstance(data, str):
data = data.encode('utf-8')
headers['Content-Type'] = 'application/json'
_json = kwargs.get('json')
# check if global rate limited
if self.global_event.is_set():
await self.global_event.wait()
# open http request with retries
async with HoldableLock(lock) as hold_lock:
async with anyio.create_task_group() as nursery:
for tries in range(self.retries):
resp = await self.session.request(method, endpoint, headers=headers, data=data, json=_json)
# get response and header data
data = resp.text
if 'application/json' in resp.headers['content-type']:
data = decoder(data)
remaining = resp.headers.get('X-Ratelimit-Remaining', 0)
# check if route should be rate limited
if remaining == '0' and resp.status_code != 429:
hold_lock.hold()
delay = int(resp.headers.get('X-Ratelimit-Reset')) - time.time()
await nursery.spawn(run_later, delay, lock.release())
# check if route IS rate limited
async def _wait_for_manager(manager, name: str, predicate):
"""
Helper class for managing a wait_for.
"""
async with anyio.create_task_group() as tg:
tg: anyio.TaskGroup
try:
partial = functools.partial(manager.wait_for, name, predicate)
await tg.spawn(partial)
yield
except:
await tg.cancel_scope.cancel()
raise
async def async_func_wrapper(*args, **kwargs):
async with all_subtasks_awaited():
return await async_func(*args, **kwargs)
return async_func_wrapper
async def __call__(self, socket):
# TODO: Should at least pass through GeneratorExit
try:
async with GRPCProtoSocket(self.config, socket) as self.grpc_socket:
# TODO: resource usage warning
# TODO: TaskGroup() uses a lot of memory if the connection is kept for a long time
# TODO: do we really need it here?
async with anyio.create_task_group() as task_group:
async for stream in self.grpc_socket.listen():
await task_group.spawn(self.request_received, stream)
except:
logging.exception("Got exception in main dispatch loop")
async def __aenter__(self):
await super().__aenter__()
task_group = await self.enter_async_context(anyio.create_task_group())
await task_group.spawn(self._writer_thread)
async def callback():
self._running = False
await self._flush_event.set()
self.push_async_callback(callback)
return self