Last active
October 11, 2017 18:31
-
-
Save vodik/3551912e4daa86c251dae25906a54ff4 to your computer and use it in GitHub Desktop.
chaining asyncio
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 | |
async def poopyface(value): | |
if value == 3: | |
raise RuntimeError('Bail') | |
print(value) | |
def runall(*awaitables, loop=None): | |
if not loop: | |
loop = asyncio.get_event_loop() | |
outer = loop. create_future() | |
tasks = iter(awaitables) | |
def chain_next_task(result): | |
exc = result.exception() | |
if exc: | |
for pending_task in tasks: | |
future = asyncio.ensure_future(pending_task) | |
future._log_destroy_pending = False | |
future.cancel() | |
outer.set_exception(exc) | |
else: | |
try: | |
future = asyncio.ensure_future(next(tasks)) | |
future._log_destroy_pending = False | |
future.add_done_callback(chain_next_task) | |
except StopIteration: | |
outer.set_result(result.done()) | |
future = asyncio.ensure_future(next(tasks)) | |
future.add_done_callback(chain_next_task) | |
return outer | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(runall(poopyface(1), poopyface(2), poopyface(3))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment