Member-only story
Asyncio for Algorithmic Trading — Part 2
Tasks, Scheduling, and What Actually Runs
In Part 1, we introduced the core model:
- Coroutines are functions that can pause
- The event loop is the scheduler
awaitis a yield point
Now we will see some practical examples.
How to Run the Examples (Quick Practical Note)¶
All examples below assume you have a main() coroutine and want to execute it.
If you’re running this in:
A normal .py script:
asyncio.run(main())Jupyter notebook:
await main()Jupyter already runs inside an active event loop, so asyncio.run() will fail there. We explain this in more details in the appendix — for now just remember:
Script →
asyncio.run(main())Jupyter →
await main()
My examples assume Jupyter so I will use await main() .
Defining a Coroutine
import asyncio
async def some_coroutine():
print("Started")
await asyncio.sleep(1)
print("Finished")Important observations:
async defdefines a coroutine function.awaitmarks a suspension point.








