Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python __await__() Magic Method

#1
Python __await__() Magic Method

The __await__() magic method defines the behavior of an awaitable object. An awaitable object is used to implement asynchronous behavior in Python.

For example, you can implement an asynchronous function that waits for the data based to access some data like so (see source):

async def read_data(db): data = await db.fetch('SELECT ...')

Syntax __await__()


object.__await__(self)

The __await__() method must return an iterator. Otherwise, it raises a TypeError.

What is await in Python?


Say, you call await do_something() in the body of the function f. In this case, function f will suspend its execution while do_something() runs. As soon as the do_something() terminates, the event loop — responsible for running asynchronous tasks — will resume the execution of function f and will pass the result to the calling environment.

The behavior of the await keyword on the awaitable object (in our case do_something()) can be defined using the __await__() method.

“A coroutine waiting for a Future-like object is suspended until the Future-like object’s __await__() completes, and returns the result.” (source)

💡 You can learn more about the concept of coroutines here.



https://www.sickgaming.net/blog/2021/11/...ic-method/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

[-]
Discord

[-]
Active Threads
News - Video: We Have A Lot Of Feelings ...
Last Post: xSicKxBot
Yesterday 11:53 PM
» Replies: 0
» Views: 13
News - Iconic Rockstar And Actor Meat Lo...
Last Post: xSicKxBot
Yesterday 11:53 PM
» Replies: 0
» Views: 1
(Indie Deal) FREE Last Horizon, Microids...
Last Post: xSicKxBot
Yesterday 08:25 PM
» Replies: 0
» Views: 10
Microsoft - Celebrating 20 years of Trus...
Last Post: xSicKxBot
Yesterday 08:25 PM
» Replies: 0
» Views: 3
Xbox Wire - New EA Play Member Rewards F...
Last Post: xSicKxBot
Yesterday 08:25 PM
» Replies: 0
» Views: 3
News - PS5 Will Outsell Series X|S 2:1 I...
Last Post: xSicKxBot
Yesterday 08:25 PM
» Replies: 0
» Views: 4
[Tut] Pandas get_dummies() – A Simple Gu...
Last Post: xSicKxBot
Yesterday 04:18 PM
» Replies: 0
» Views: 19
Mobile - Genshin Impact’s Tomo – vision,...
Last Post: xSicKxBot
Yesterday 04:18 PM
» Replies: 0
» Views: 5
News - Community Focus: Parris
Last Post: xSicKxBot
Yesterday 04:18 PM
» Replies: 0
» Views: 8
News - Bedrock: BETA 1.18.10.28
Last Post: xSicKxBot
Yesterday 04:18 PM
» Replies: 0
» Views: 6

[-]
Twitter



Discord Server © SickGaming.net 2012-2021