Last active
August 11, 2016 11:29
-
-
Save sirkonst/d3dd92ee97083fc9d7e20785606d5dcc to your computer and use it in GitHub Desktop.
Сlosure with asyncio corrutines isn't async-safe
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
import asyncio | |
from functools import partial | |
async def process(d): | |
print('process', d) | |
async def amain(loop): | |
data = range(2) | |
fs = [] | |
for d in data: | |
f = loop.create_task(process(d)) | |
fs.append(f) | |
# - This callback is closured wrong value for d | |
def _cb_broken(future): | |
print('_cb_broken', d) | |
f.add_done_callback(_cb_broken) | |
# - Fix 1 | |
def _cb_broken_fix_1(future, d=d): | |
print('_cb_broken_fix_1', d) | |
f.add_done_callback(_cb_broken_fix_1) | |
# - Fix 2 | |
def _cb_broken_fix_2(future, d): | |
print('_cb_broken_fix_2', d) | |
f.add_done_callback( | |
partial(_cb_broken_fix_2, d=d) | |
) | |
await asyncio.wait(fs) | |
if __name__ == '__main__': | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(amain(loop)) | |
# | |
# Run output: | |
# | |
# process 0 | |
# process 1 | |
# _cb_broken 1 | |
# _cb_broken_fix_1 0 | |
# _cb_broken_fix_2 0 | |
# _cb_broken 1 | |
# _cb_broken_fix_1 1 | |
# _cb_broken_fix_2 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment