Telegram Group & Telegram Channel
When talking about asyncio functions, sometimes I used the word "coroutine" and sometimes "task". It's time to tell you the difference:

+ coroutine is what async function returns. It can be scheduled, switched, closed, and so on. It's quite similar to generators. In fact, await keyword is nothing more than an alias for yield from, and async is a decorator turning the function from a generator into a coroutine.

+ asyncio.Future is like "promise" in JS. It is an object that eventually will hold a coroutine result when it is available. It has done method to check if the result is available, result to get the result, and so on.

+ asyncio.Task is like if coroutine and future had a baby. This is what asyncio mostly works with. It can be scheduled, switched, canceled, and holds its result when ready.

There is a cool function asyncio.create_task that can turn a coroutine into a proper task. What's cool about it is that this task immediately gets scheduled. So, if your code later encounters await, there is a chance your task will be executed at that point.

import asyncio

async def child():
print('started child')
await asyncio.sleep(1)
print('finished child')

async def main():
asyncio.create_task(child())
print('before sleep')
await asyncio.sleep(0)
print('after sleep')

asyncio.run(main())


Output:

before sleep
started child
after sleep


What happened:

1. When create_task is called, it is scheduled but not yet executed.
2. When main hits await, the scheduler switches to child.
3. When child hits await, the scheduler switches to another task, which is main
4. When main finished, asyncio.run returned without waiting for child to finish. It's dead in space now.

But what if you want to make sure a scheduled task finishes before exiting? You can pass the task into good old asyncio.gather. And later we'll see some ways to wait for it with timeouts or when you don't care about the result.

task = create_task(...)
...
await asyncio.gather(task)



tg-me.com/pythonetc/724
Create:
Last Update:

When talking about asyncio functions, sometimes I used the word "coroutine" and sometimes "task". It's time to tell you the difference:

+ coroutine is what async function returns. It can be scheduled, switched, closed, and so on. It's quite similar to generators. In fact, await keyword is nothing more than an alias for yield from, and async is a decorator turning the function from a generator into a coroutine.

+ asyncio.Future is like "promise" in JS. It is an object that eventually will hold a coroutine result when it is available. It has done method to check if the result is available, result to get the result, and so on.

+ asyncio.Task is like if coroutine and future had a baby. This is what asyncio mostly works with. It can be scheduled, switched, canceled, and holds its result when ready.

There is a cool function asyncio.create_task that can turn a coroutine into a proper task. What's cool about it is that this task immediately gets scheduled. So, if your code later encounters await, there is a chance your task will be executed at that point.

import asyncio

async def child():
print('started child')
await asyncio.sleep(1)
print('finished child')

async def main():
asyncio.create_task(child())
print('before sleep')
await asyncio.sleep(0)
print('after sleep')

asyncio.run(main())


Output:

before sleep
started child
after sleep


What happened:

1. When create_task is called, it is scheduled but not yet executed.
2. When main hits await, the scheduler switches to child.
3. When child hits await, the scheduler switches to another task, which is main
4. When main finished, asyncio.run returned without waiting for child to finish. It's dead in space now.

But what if you want to make sure a scheduled task finishes before exiting? You can pass the task into good old asyncio.gather. And later we'll see some ways to wait for it with timeouts or when you don't care about the result.

task = create_task(...)
...
await asyncio.gather(task)

BY Python etc


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 280

Share with your friend now:
tg-me.com/pythonetc/724

View MORE
Open in Telegram


Pythonetc Telegram | DID YOU KNOW?

Date: |

How Does Bitcoin Mining Work?

Bitcoin mining is the process of adding new transactions to the Bitcoin blockchain. It’s a tough job. People who choose to mine Bitcoin use a process called proof of work, deploying computers in a race to solve mathematical puzzles that verify transactions.To entice miners to keep racing to solve the puzzles and support the overall system, the Bitcoin code rewards miners with new Bitcoins. “This is how new coins are created” and new transactions are added to the blockchain, says Okoro.

Telegram Be The Next Best SPAC

I have no inside knowledge of a potential stock listing of the popular anti-Whatsapp messaging app, Telegram. But I know this much, judging by most people I talk to, especially crypto investors, if Telegram ever went public, people would gobble it up. I know I would. I’m waiting for it. So is Sergei Sergienko, who claims he owns $800,000 of Telegram’s pre-initial coin offering (ICO) tokens. “If Telegram does a SPAC IPO, there would be demand for this issue. It would probably outstrip the interest we saw during the ICO. Why? Because as of right now Telegram looks like a liberal application that can accept anyone - right after WhatsApp and others have turn on the censorship,” he says.

Pythonetc from cn


Telegram Python etc
FROM USA