Demystifying Python's Async and Await Keywords

JetBrains · 104K views · 2019 · 1:18:53

Open on YouTube

Python, Ruby & scripting languagesSoftware practice, open source & careerstutorial

Why Stack Overflow recommends it

Stack Overflow users link it when explaining how asyncio works and when pointing people toward the unsync library for mixing sync and async code.

Recommended by 2 people across 2 different months, 2018 to 2021.

What it is

A 78-minute JetBrains webinar by Michael Kennedy (host of Talk Python to Me and Python Bytes) on Python's async and await keywords. It surveys Python's parallel APIs, builds a small app that calls web APIs with asyncio, and ends with the unsync library for unifying the different concurrency models.

What people said

Recommends pylang · 2018-07 · Stack Overflow · on “Sync code to async, without rewriting the function”

Here are two options mixing synchronous and asynchronous code. Given import asyncio import trio from unsync import unsync Code Option 1 - trio [LINK] Given sync code ( call ), call an async function via trio ( async_div ): # Sync code def call(x, y): """Return an awaited result.""" try: resp = async_div(x, y) except ZeroDivisionError: return "Caught an exception." return resp # Wrapper def async_div(*args): """Return results from an async function.""" return trio.run(_async_div, *args) # Async code async def _async_div(x, y): """Return an async result.""" await trio.sleep(3) return x / y Option 2 - unsync [LINK] Decorate the async code with @unsync and call the result() : # Sync code def cal

Recommends Maciek · 2021-04 · Stack Overflow · on “python3.6 async/await still works synchronously with fastAPI”

Explanation The reason your code run synchronyously is that in do_things function, the code is executed as follow: Schedule search_object.search() to execute Wait till search_object.search() is finished and get the result Schedule search_object.search() to execute Wait till search_object.search() is finished and get the result Execute (synchronyously) process(resp2) Execute (synchronyously) process(resp1) Execute (synchronyously) do_synchronous_things() What you intended, is to make steps 1 and 3 executed before 2 and 4. You can make it easily with unsync library - here is the documentation [LINK] . How you can fix this from unsync import unsync class my_module: search_object = AsyncElastics

Cites paxdiablo · 2021-09 · Stack Overflow · on “How to handle tasks with varying completion times”

I'm watching a video (a) on YouTube about asyncio and, at one point, code like the following is presented for efficiently handling multiple HTTP requests: # Need an event loop for doing this. loop = asyncio.get_event_loop() # Task creation section. tasks = [] for n in range(1, 50): tasks.append(loop.create_task(get_html(f" [LINK] "))) # Task processing section. for task in tasks: html = await task thing = get_thing_from_html(html) print(f"Thing found: {thing}", flush=True) I realise that this is efficient in the sense that everything runs concurrently but what concerns me is a case like: the first task taking a full minute; but all the others finishing in under three seconds. Because the tas