K
K
kokapuk2021-08-25 00:08:16
Python
kokapuk, 2021-08-25 00:08:16

What is the difference in designs?

import asyncio
import time

async def say_after(delay, what):
    await asyncio.sleep(delay)
    print(what)

async def main():
    print(f"started at {time.strftime('%X')}")

    await say_after(1, 'hello')
    await say_after(2, 'world')

    print(f"finished at {time.strftime('%X')}")

asyncio.run(main())

-------------------------------------------------- -
import time

def say_after(delay, what):
    time.sleep(delay)
    print(what)

def main():
    print(f"started at {time.strftime('%X')}")

    say_after(1, 'hello')
    say_after(2, 'world')

    print(f"finished at {time.strftime('%X')}")

main()

I took the code above from the official python documentation, I can’t understand the meaning of this construction, because, for example, in the same C Sharp in an async method, it was possible to launch a method through await, which did not block the rest when.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2021-08-25
@kokapuk

I don’t know what you have in Sea Sharp, but here is an excerpt from the official Sea Sharp documentation :

The await operator suspends evaluation of the enclosing async method until the asynchronous operation represented by its operand completes.

That is, as in all languages ​​(including python), await suspends evaluation of the enclosing async method and waits for the completion of the coroutine

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question