This file contains 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
async def main_concurrent(): | |
t = time.perf_counter() | |
tasks = [asyncio.create_task(handle_url(url)) | |
for url in urls] | |
## All of the variants below would wait for everything: | |
# V1: this will wait as little as possible | |
# (point of this is to show that tasks start executing when created) | |
# for task in tasks: | |
# await task | |
# V2: ensures all is done by waiting for longest possible time | |
# await asyncio.sleep(11) | |
# V3: the kind of code one could actually write in real life | |
# (but it's a bit redundant: `wait` or `gather` can also receive | |
# coroutines and can start and run them properly) | |
await asyncio.wait(tasks) | |
print("> extracted data:", extracted_data) | |
print(f"time elapsed: {time.perf_counter() - t:.2f}s") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment