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 | |
import logging | |
import time | |
async def do_work(i): | |
print('start', i) | |
# Work | |
time.sleep(1) |
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
# From http://stackoverflow.com/a/18422264 | |
import subprocess | |
import sys | |
with open('test.log', 'w') as f: | |
process = subprocess.Popen(your_command, stdout=subprocess.PIPE) | |
for line in iter(process.stdout.readline, ''): # With Python 3, you need iter(process.stdout.readline, b'') (i.e. the sentinel passed to iter needs to be a binary string, since b'' != '') | |
sys.stdout.write(line) | |
f.write(line) |
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 contextlib import closing | |
import random | |
async def async_index_printer(index: int): | |
print('start', index) | |
await asyncio.sleep(random.uniform(1, 3)) | |
return index |
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 | |
import itertools | |
def as_completed_with_max_concurrent(futures, max_concurrent, loop=None, timeout=None): | |
"""Tweaked version of `asyncio.as_completed` with the addition of the `max_concurrent` param. | |
The main change is to only queue (`_queue_future`) the first `max_concurrent` futures initially. | |
The rest will be queued in `_on_completion`. | |
""" |
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 contextlib import closing | |
import aiohttp | |
async def download_file(session: aiohttp.ClientSession, url: str): | |
async with session.get(url) as response: | |
assert response.status == 200 | |
# For large files use response.content.read(chunk_size) instead. |
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
def coroutine(func): | |
def start(*args, **kwargs): | |
cr = func(*args, **kwargs) | |
next(cr) | |
return cr | |
return start | |
@coroutine | |
def broadcast(*sinks): |
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 argparse | |
import os | |
def directory(raw_path): | |
if not os.path.isdir(raw_path): | |
raise argparse.ArgumentTypeError('"{}" is not an existing directory'.format(raw_path)) | |
return os.path.abspath(raw_path) | |