![](https://secure.gravatar.com/avatar/aeb9c7939577ccaadb4b31775f7491c4.jpg?s=120&d=mm&r=g)
my question is 1st one is concurrent but 2nd one is not, how and please correct me, what i miss and what should i know more thank you import asyncio # 1st code async def say(what, when): await asyncio.sleep(when) print(what) loop = asyncio.get_event_loop() loop.create_task(say('first hello', 2)) loop.create_task(say('second hello', 1)) loop.run_forever() loop.close() ''' result
second hello first hello '''
# 2nd code async def say(what, when): await asyncio.sleep(when) print(what) async def main(loop): yield from loop.create_task(say('first hello', 2)) yield from loop.create_task(say('second hello', 1)) print('close') loop = asyncio.get_event_loop() loop.run_until_complete(main(loop)) loop.close() ''' result
first hello second hello '''
![](https://secure.gravatar.com/avatar/e8600d16ba667cc8d7f00ddc9f254340.jpg?s=120&d=mm&r=g)
It's because you're awaiting on your tasks in your 2nd example, causing you to make your main() call wait until each task is complete before moving on (notice how you don't await in your calls to loop.create_task() in your 1st example). I think you want is something like: import asyncio async def say(what, when): await asyncio.sleep(when) print(what) async def main(loop): task1 = loop.create_task(say('first hello (sleep 2 seconds)', 2)) task2 = loop.create_task(say('second hello (sleep one second)', 1)) await asyncio.gather(task1, task2, loop=loop) print('close') loop = asyncio.get_event_loop() loop.run_until_complete(main(loop)) loop.close() On Mon, 27 Aug 2018 at 02:47 saurabh singh <saurabh3460@gmail.com> wrote:
my question is 1st one is concurrent but 2nd one is not, how and please correct me, what i miss and what should i know more thank you
import asyncio
# 1st code async def say(what, when): await asyncio.sleep(when) print(what)
loop = asyncio.get_event_loop()
loop.create_task(say('first hello', 2)) loop.create_task(say('second hello', 1))
loop.run_forever() loop.close()
''' result
second hello first hello '''
# 2nd code async def say(what, when): await asyncio.sleep(when) print(what)
async def main(loop): yield from loop.create_task(say('first hello', 2)) yield from loop.create_task(say('second hello', 1)) print('close')
loop = asyncio.get_event_loop() loop.run_until_complete(main(loop)) loop.close()
''' result
first hello second hello '''
_______________________________________________ Async-sig mailing list Async-sig@python.org https://mail.python.org/mailman/listinfo/async-sig Code of Conduct: https://www.python.org/psf/codeofconduct/
![](https://secure.gravatar.com/avatar/fbeee3d76a35f9de8847f36ee51c28a2.jpg?s=120&d=mm&r=g)
What Bret said, here (perhaps) more concise: async def main(): f1 = ensure_future(say("two", 2)) f2 = ensure_future(say("one", 1)) # at this point both are running await f1 await f2 Note that current event loop is automatic since Python 3.6; Futures are higher level abstraction, but I think it's better to start with futures :) On Mon, 27 Aug 2018 at 5:47 PM, saurabh singh <saurabh3460@gmail.com> wrote:
my question is 1st one is concurrent but 2nd one is not, how and please correct me, what i miss and what should i know more thank you
import asyncio
# 1st code async def say(what, when): await asyncio.sleep(when) print(what)
loop = asyncio.get_event_loop()
loop.create_task(say('first hello', 2)) loop.create_task(say('second hello', 1))
loop.run_forever() loop.close()
''' result
second hello first hello '''
# 2nd code async def say(what, when): await asyncio.sleep(when) print(what)
async def main(loop): yield from loop.create_task(say('first hello', 2)) yield from loop.create_task(say('second hello', 1)) print('close')
loop = asyncio.get_event_loop() loop.run_until_complete(main(loop)) loop.close()
''' result
first hello second hello '''
_______________________________________________ Async-sig mailing list Async-sig@python.org https://mail.python.org/mailman/listinfo/async-sig Code of Conduct: https://www.python.org/psf/codeofconduct/
![](https://secure.gravatar.com/avatar/7775d42d960a69e98fecf270bdeb6f57.jpg?s=120&d=mm&r=g)
Actually future is low level abstraction :) On Tue, Aug 28, 2018 at 2:31 PM Dima Tisnek <dimaqq@gmail.com> wrote:
What Bret said, here (perhaps) more concise:
async def main(): f1 = ensure_future(say("two", 2)) f2 = ensure_future(say("one", 1)) # at this point both are running await f1 await f2
Note that current event loop is automatic since Python 3.6; Futures are higher level abstraction, but I think it's better to start with futures :)
On Mon, 27 Aug 2018 at 5:47 PM, saurabh singh <saurabh3460@gmail.com> wrote:
my question is 1st one is concurrent but 2nd one is not, how and please correct me, what i miss and what should i know more thank you
import asyncio
# 1st code async def say(what, when): await asyncio.sleep(when) print(what)
loop = asyncio.get_event_loop()
loop.create_task(say('first hello', 2)) loop.create_task(say('second hello', 1))
loop.run_forever() loop.close()
''' result
second hello first hello '''
# 2nd code async def say(what, when): await asyncio.sleep(when) print(what)
async def main(loop): yield from loop.create_task(say('first hello', 2)) yield from loop.create_task(say('second hello', 1)) print('close')
loop = asyncio.get_event_loop() loop.run_until_complete(main(loop)) loop.close()
''' result
first hello second hello '''
_______________________________________________ Async-sig mailing list Async-sig@python.org https://mail.python.org/mailman/listinfo/async-sig Code of Conduct: https://www.python.org/psf/codeofconduct/
_______________________________________________ Async-sig mailing list Async-sig@python.org https://mail.python.org/mailman/listinfo/async-sig Code of Conduct: https://www.python.org/psf/codeofconduct/
-- Thanks, Andrew Svetlov
participants (4)
-
Andrew Svetlov
-
Brett Cannon
-
Dima Tisnek
-
saurabh singh