Title: Theory / Unimportant / CS / Programming / Python / Async / General
Tags: automatedNewLineAfterCommaSpace FromInstruction computer_to_instruction
On every note of this deck, unless a note says otherwise:
Page: https://docs.python.org/3.10/library/asyncio-task.html

Instruction: isawaitable(foo)
Page: https://docs.python.org/3/library/inspect.html
Language: Python
Context: Inspect
Returns: Can foo be used in await expression

Function: __await__ Must return an iterator.
Page: https://docs.python.org/3/reference/datamodel.html#awaitable-objects
Name: Awaitable

Function: send
Name: Coroutine object
Function2: throw
Extends: Awaitable
Intuition: Result of calling a coroutine function

Name: asyncio
Definition: A library to write concurrent code using the async/await syntax.
Page: https://docs.python.org/3.10/library/asyncio.html
Context: python

Instruction: async def function_name(args):
  foo
Page: https://docs.python.org/3.10/library/asyncio-task.html
Meaning: same as
def function_name(args):
  foo
but the function can be awaited and need to be called in `asyncio.run` to be executed
Language: python
Name: A coroutine function

Question: How to run a coroutine
Answer: await it
Page: https://docs.python.org/3.10/library/asyncio-task.html
Section: python
Answer2: asyncio.run
Answer3: asyncio.create_task

Instruction: await coroutine
Effect: execute the coroutine and wait until it ends
Language: python
Variables: coroutine a coroutine
Where to use it: in an `async def`

Instruction: asyncio.run(coroutine)
Effect: run the coroutine and waits until it ends
Language: python
Variables: coroutine a coroutine
Assuming: not called while another one run. Ideally a single call at most in the program execution.

Instruction: asyncio.create_task(
  coro,
  *,
  name=None
)
Effect: A task named name that runs coro in background soon.
Language: python
Variables: coro a coroutine
Assuming: A reference to it is saved to avoid garbage collection

Question: the main types of awaitable objects
Answer: coroutine
Page: https://docs.python.org/3.10/library/asyncio-task.html
Answer2: task
Answer3: future

Instruction: asyncio.sleep(
  delay,
  result=None
)
Effect: wait delay seconds, suspends the task and allow other tasks to run
Language: result
Typ: asyncio.Future

Instruction: asyncio.gather(
  awaitable_1,
  ...,
  awaitable_n,
  return_exception=False
)
Effect: run all awaitable concurrently.
Language: python
Returns: list of result of awaitable_1, ..., awaitable_n. Or exception raised if return_exception=True
Extra: If gather(
) is cancelled,
  all submitted awaitables (
  that have not completed yet
) are also cancelled.
If any Task or Future from the aws sequence is cancelled,
  it is treated as if it raised CancelledError – the gather(
) call is not cancelled in this case. This is to prevent the cancellation of one submitted Task/Future to cause other Tasks/Futures to be cancelled.
Typ: asyncio.Future
Exception: exception thrown by an awaitable if return_exception=False

Instruction: asyncio.shield(aw)
Language: python
Returns: an awaitable, similar to shield but who don't propagate cancellation request to aw
Typ: asyncio.Future
Exception: exception thrown by an awaitable if return_exception=False

Instruction: asyncio.wait_for(
  aw,
  timeout
)
Effect: execute aw as a task.
Language: python
Returns: aw's return
Typ: asyncio.Future
Exception: asyncio.TimeoutError if it did not return after timeout ms
any exception of cancellation

Instruction: asyncio.wait(
  aws,
  *,
  timeout=None,
  return_when=ALL_COMPLETED|FIRST_EXCEPTION|ALL_COMPLETED
)
Effect: run all awaitables of aws concurrently
Language: python
Returns: pair done/pending, of tasks that are done/pending when the return occured. At must after timeout seconds, or after return_when occurred
Extra: awaitable must not be coroutine
Typ: asyncio.Future

Instruction: asyncio.as_completed(
  aws,
  *,
  timeout=None
)
Language: python
Returns: awaitable iterator with the result of each awaitable tasks
Exception: asyncio.TimeoutError if the timeout occurs before all Futures are done.

Instruction: asyncio.to_thread(
  func,
  /,
  *args,
  **kwargs
)
Effect: coroutine that run `fun(*args, **kwargs)`
Language: python

Instruction: asyncio.run_coroutine_threadsafe(
  coro,
  loop
)
Language: python
Returns: a future that wait for the result of coro executed on loop

Instruction: asyncio.current_tasks(loop=None)
Language: python
Returns: The currently running Task instance on loop or current loop, or None if no task is running.

Instruction: asyncio.all_tasks(loop=None)
Language: python
Returns: a set of not yet finished Task objects run by the loop (or current loop).

Typ: asyncio.Task(coro, *, loop=None, name=None)
Meaning: A Future-like object that runs a Python coroutine. Not thread-safe.
Page: https://docs.python.org/3.10/library/asyncio-task.html
Language: python

Typ: asyncio_queue.CancelledError
Meaning: The operation has been cancelled.
Page: https://docs.python.org/3.10/library/asyncio-queue.html
Language: python
Extra: This exception can be caught to perform custom operations when asyncio Tasks are cancelled. In almost all situations the exception must be re-raised.

Typ: asyncio_queue.InvalidStateError
Meaning: Invalid internal state of Task or Future.
Can be raised in situations like setting a result value for a Future object that already has a result value set.
Page: https://docs.python.org/3.10/library/asyncio-queue.html
Language: python
Extra: This exception can be caught to perform custom operations when asyncio Tasks are cancelled. In almost all situations the exception must be re-raised.

Question: what do debug mode add to asyncio
Answer: warn if a coroutine is not awaited for
Page: https://docs.python.org/3.10/library/asyncio-dev.html
Context: Python
Answer2: warn if a future's exception is set and never retrieved