[Async-sig] concurrency mistake

saurabh singh saurabh3460 at gmail.com
Mon Aug 27 05:46:40 EDT 2018


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
'''
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/async-sig/attachments/20180827/5f51aaf0/attachment.html>


More information about the Async-sig mailing list