Created
April 26, 2018 02:44
-
-
Save bonfy/ddaf85e1878b885091231328023021f3 to your computer and use it in GitHub Desktop.
async example modify a global dict
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
import asyncio | |
g = {'num': 1} | |
async def add(g, num=1): | |
print(f'origin {num} from', g['num']) | |
await asyncio.sleep(10) | |
print(f'do add {num} from', g['num']) | |
g['num'] += num | |
_funcs = (add(g, 1), add(g,2), add(g,5)) | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(asyncio.gather(*_funcs)) | |
loop.close() | |
print(g) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment