Skip to content

Latest commit

 

History

History
44 lines (27 loc) · 1.22 KB

async_usage.rst

File metadata and controls

44 lines (27 loc) · 1.22 KB

Async Usage

If you use an async transport <async_transports>, you can use GQL asynchronously using asyncio.

  • put your code in an asyncio coroutine (method starting with async def)
  • use async with client as session: to connect to the backend and provide a session instance
  • use the await keyword to execute requests: await session.execute(...)
  • then run your coroutine in an asyncio event loop by running asyncio.run

Example:

../code_examples/aiohttp_async.py

IPython

Warning

On some Python environments, like Jupyter or Spyder, which are using IPython, an asyncio event loop is already created for you by the environment.

In this case, running the above code might generate the following error:

RuntimeError: asyncio.run() cannot be called from a running event loop

If that happens, depending on the environment, you should replace asyncio.run(main()) by either:

await main()

OR:

loop = asyncio.get_running_loop()
loop.create_task(main())