How do I run a coroutine, and what do I need before I can?
Point it at the documentation that should hold the answer, and ask. It reads the pages and writes the answer with the section heading behind every line.
The answer, as it printed.
Every claim names the page and the heading it came from, which is what makes an answer worth pasting into a pull request.
How do I run a coroutine, and what do I need before I can?
To run a coroutine, you can use one of these mechanisms:
- Use asyncio.run(coro) to execute the coroutine in a new event loop and return its result. The argument can be any awaitable object. This function should be used as the main entry point for asyncio programs and cannot be called when another event loop is running in the same thread [2], "Running an asyncio Program".
- Await the coroutine from within another coroutine using the await keyword [1], "Coroutines".
- Use asyncio.create_task(coro) to schedule the coroutine as a Task for concurrent execution within an event loop. You must have a running event loop in the current thread, or a RuntimeError is raised [1], "Creating tasks".
- Use asyncio.TaskGroup (Python 3.11+) as an async context manager to create and manage multiple tasks [1], "Task groups".
Before you can run a coroutine:
- You need an event loop running in the current thread. asyncio.run() and asyncio.Runner() will create and manage the event loop for you. If you use create_task(), you must already be inside a running event loop (e.g., inside another coroutine) [1], "Creating tasks"; [2], "Running an asyncio Program".
- If you use asyncio.run(), you cannot already have another event loop running in the same thread [2], "Running an asyncio Program".
Example using asyncio.run():
```python
import asyncio
async def main():
print('hello')
await asyncio.sleep(1)
print('world')
asyncio.run(main())
```
[1], "Coroutines"; [2], "Running an asyncio Program"
Example using create_task() inside a coroutine:
```python
import asyncio
async def say_after(delay, what):
await asyncio.sleep(delay)
print(what)
async def main():
task1 = asyncio.create_task(say_after(1, 'hello'))
task2 = asyncio.create_task(say_after(2, 'world'))
await task1
await task2
asyncio.run(main())
```
[1], "Coroutines"
Summary:
- Use asyncio.run() or asyncio.Runner().run() for top-level entry.
- Use await or create_task() inside a running event loop.
- You need a running event loop, which asyncio.run() provides, or you must already be inside one for create_task().
[1], "Coroutines", "Creating tasks"; [2], "Running an asyncio Program", "Runner context manager"
Sources
[1] Coroutines and tasks https://docs.python.org/3/library/asyncio-task.html
[2] Runners https://docs.python.org/3/library/asyncio-runner.html- [1]Coroutines and taskshttps://docs.python.org/3/library/asyncio-task.html
- [2]Runnershttps://docs.python.org/3/library/asyncio-runner.html
The documentation pages that should hold the answer, with the most likely page first.
- https://docs.python.org/3/library/asyncio-task.html
- https://docs.python.org/3/library/asyncio-runner.html
The answer in plain words with the page and the section heading it came from, and a plain 'the docs given do not say' when the pages do not answer it.
By hand this is the site's own search box, six tabs, and a paragraph you are not certain is current.
The command that produced all of this.
Paste it as it is and watch it work, then swap in your own pages. With your Lyrenth key set, it prints the finished prompt and its numbered sources, ready for whichever assistant you already use. Point it at an OpenAI-compatible endpoint and it answers on its own, the way the run above did.
Research brief
Give it the pages to read and get an answer that shows where every part came from.
See what it does →Article summary
Hand over a long article and get the argument in a few hundred words: what it claims, what it rests on, and what it leaves out.
See what it does →Table from pages
Give it several similar pages and get one table, a row per page, with an empty cell wherever a page did not say.
See what it does →