Skip to content

Instantly share code, notes, and snippets.

@bonfy
Created April 26, 2018 02:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bonfy/ddaf85e1878b885091231328023021f3 to your computer and use it in GitHub Desktop.
Save bonfy/ddaf85e1878b885091231328023021f3 to your computer and use it in GitHub Desktop.
async example modify a global dict
# 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