User Tools

Site Tools


python:asyncio

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
python:asyncio [2025/12/15 08:59] v1ctorpython:asyncio [2025/12/15 13:02] (current) v1ctor
Line 5: Line 5:
 ''async'' - let us to define a coroutine.\\ ''async'' - let us to define a coroutine.\\
 ''await'' - let us pause our coroutine when we have a long-running operation. Blocking operation.\\ ''await'' - let us pause our coroutine when we have a long-running operation. Blocking operation.\\
 +
 +==== TASKS ====
 +
 ''Task'' - wraps a coroutine and schedules it to run on the event loop immediately (in the background). The task starts running as soon as the event loop gets a chance (e.g., when you next await something; await triggers an iteration of the event loop).\\ ''Task'' - wraps a coroutine and schedules it to run on the event loop immediately (in the background). The task starts running as soon as the event loop gets a chance (e.g., when you next await something; await triggers an iteration of the event loop).\\
 +
 +Task can be cancelled:
 +<code python>
 +long_task = asyncio.create_task(delay(10))
 +while not long_task.done():
 +  long_task.cancel()
 +
 +try:
 +    await long_task
 +except CancelledError:
 +    print("Our task was cancelled")
 +</code>
python/asyncio.1765789154.txt.gz · Last modified: by v1ctor