Files
hatchet/examples/python/blocked_async/debugging.py
Matt Kaye f9d3d508ca Blocked event loop blog post (#1775)
* feat: snippets

* feat: first couple paragraphs

* feat: flesh out hatchet examples

* fix: one more

* feat: log view

* fix: cleanup

* feat: another section

* fix: fmt

* feat: debugging section

* fix: proofread

* fix: lint

* chore: gen

* fix: copilot

* fix: copilot

* feat: add blog post link

* fix: feedback

* chore: sdk ver

* chore: gen

* fix: ugh
2025-05-27 11:07:34 -07:00

53 lines
869 B
Python

# > Functions
import asyncio
import time
SLEEP_TIME = 3
async def blocking() -> None:
for i in range(SLEEP_TIME):
print("Blocking", i)
time.sleep(1)
async def non_blocking(task_id: str = "Non-blocking") -> None:
for i in range(SLEEP_TIME):
print(task_id, i)
await asyncio.sleep(1)
# > Blocked
async def blocked() -> None:
loop = asyncio.get_event_loop()
await asyncio.gather(
*[
loop.create_task(blocking()),
loop.create_task(non_blocking()),
]
)
# > Unblocked
async def working() -> None:
loop = asyncio.get_event_loop()
await asyncio.gather(
*[
loop.create_task(non_blocking("A")),
loop.create_task(non_blocking("B")),
]
)
if __name__ == "__main__":
asyncio.run(blocked())
asyncio.run(working())