How to use the anyio.create_task_group function in anyio

To help you get started, we’ve selected a few anyio examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github clamor-py / anysocks / tests / test_connection.py View on Github external
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)
github agronholm / anyio / tests / test_taskgroups.py View on Github external
async def testfunc():
        async with create_task_group() as tg:
            await tg.spawn(sleep, 0)
github standy66 / purerpc / tests / test_client_server_codegen.py View on Github external
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)
github standy66 / purerpc / tests / test_client_server_codegen.py View on Github external
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)
github standy66 / purerpc / tests / test_client_server_codegen.py View on Github external
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)
github kyb3r / pycord / pycord / api / http.py View on Github external
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
github Fuyukai / curious / curious / core / event / manager.py View on Github external
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
github sanitizers / octomachinery / octomachinery / utils / asynctools.py View on Github external
async def async_func_wrapper(*args, **kwargs):
        async with all_subtasks_awaited():
            return await async_func(*args, **kwargs)
    return async_func_wrapper
github standy66 / purerpc / src / purerpc / server.py View on Github external
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")
github standy66 / purerpc / src / purerpc / grpc_socket.py View on Github external
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