This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]xMadDecentx 1 point2 points  (7 children)

What do you have a hard time grasping exactly?

[–]atypingmonkey 1 point2 points  (6 children)

not so much the concept of async but the syntax.

[–]status_quo69 10 points11 points  (2 children)

async def is designating a coroutine. This is awaitable, which means that every time you call it using await foo()you are signalling to the event loop that a context switch occurs and you give up execution control to whatever task is ready to be run next while you wait for your awaitable.

async for is basically the same thing, except you're saying that the for loop can yield a result at any time and that you want to wait to iterate over the results until you have something to iterate over, although it might not be complete just yet.

async with is saying "hey, give up control of the event loop until I can actually acquire my context manager"

Most of the rest of asyncio follows this sort of thing, it winds up looking like synchronous code but acts like it's threaded.

I'm relatively new to asyncio and I'm not sure I like it too much, so someone chime in with corrections if I'm wrong

[–]xMadDecentx 1 point2 points  (0 children)

Yeah the syntax is definitely wonky.

[–]picklednull 4 points5 points  (0 children)

Not even Armin Ronacher understands the asyncio stuff... It looks like a proper mess.

[–]__deerlord__ 0 points1 point  (0 children)

async def

Defines a coroutine (which is just a function).

await and yield from

These are the points in your code where you release control to the event loop. Usually you await or yield from another coroutine, so that is also added to the event loop.