Skip to content

Latest commit

 

History

History
53 lines (43 loc) · 2.22 KB

background.org

File metadata and controls

53 lines (43 loc) · 2.22 KB

Python background

async

In the tutorials, you can safely ignore the presence of the “async” and “await” keywords. We don’t use any actual async features in the tutorials, so everything we’re doing is equivalent to regular, non-async Python.

Python 3 async/await syntax looks like this:

async def f(x):
    return await some_library_function(x, x)

async def main():
    print(await f("hello"))

Any async function f (a function defined with async def f(args)) has to be called with await f(args). await can only be used in an async function, so async functions can only be called by async functions.

The main async function is run by a special non-async function provided by an async library (such as trio or asyncio). At the top level of an rsyscall program is therefore a call to trio.run or asyncio.run.

Async functions are, from the user perspective, equivalent to normal functions. Only when using async library features does a difference appear, which, as previously mentioned, we don’t do in the tutorials. rsyscall uses Python async/await primarily so that users can use async/await, not because rsyscall gets much utility out of it.

For more information on async/await syntax in Python, check out the trio tutorial. While rsyscall uses trio, it isn’t necessary or useful to know anything about the trio library itself to use rsyscall. As shown in the single-threaded tutorial, rsyscall provides its own interfaces for IO, and makes no use of trio’s interfaces for IO.

types

You can safely ignore the Python 3 type annotations, both in the tutorials and in general; in Python, type annotations are purely for use by static analyzers, and have no runtime significance.

Python 3 type annotations appear in function declarations like this:

def f(x: int, y: str, z: t.List[str]) -> str: ...

This function f takes three arguments, an int (x: int), a string (y: str), and a list of strings (z: t.List[str]), and returns another string (-> str).

For more information on type annotations in Python, check out the mypy manual.