Small utility for asynchronous coroutine generic programming in Python +3.4.
Project description
paco
Small and idiomatic utility library for coroutine-driven, asynchronous-oriented generic programming in Python +3.4.
Built on top of asyncio, paco provides missing capabilities from Python stdlib to write asynchronous cooperative multitasking in a nice-ish way, plus higher-order function goodness.
paco can be your utility belt to deal with I/O-bound non-blocking concurrent code in a better and elegant way.
Features
Simple and idiomatic API, extending Python stdlib with async coroutines gotchas.
Built-in configurable control-flow concurrency support.
Useful iterables, decorators, functors and convenient helpers.
Coroutine-based functional helpers: compose, throttle, partial, timeout, times, until, race…
Asynchronous coroutine port of Python built-in functions: filter, map, dropwhile, filterfalse, reduce…
Concurrent iterables and higher-order functions.
Better asyncio.gather() and asyncio.wait() with optional concurrency control and ordered results.
Works with both async/await and yield from coroutines syntax.
Designed for intensive I/O-bound concurrent non-blocking tasks.
Good interoperability with asyncio and Python stdlib functions.
Composable pipelines of functors for iterables via | operator overloading.
Small and dependency free.
Compatible with Python +3.4.
Installation
Using pip package manager:
pip install paco
Or install the latest sources from Github:
pip install -e git+git://github.com/h2non/paco.git#egg=paco
API
Examples
Asynchronously and concurrently execute multiple HTTP requests.
import paco
import aiohttp
async def fetch(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as res:
return res
async def fetch_urls():
urls = [
'https://www.google.com',
'https://www.yahoo.com',
'https://www.bing.com',
'https://www.baidu.com',
'https://duckduckgo.com',
]
# Map concurrent executor with concurrent limit of 3
responses = await paco.map(fetch, urls, limit=3)
for res in responses:
print('Status:', res.status)
# Run in event loop
paco.run(fetch_urls())
Concurrent pipeline-style composition of functors over an iterable object.
import paco
async def filterer(x):
return x < 8
async def mapper(x):
return x * 2
async def drop(x):
return x < 10
async def reducer(acc, x):
return acc + x
async def task(numbers):
return await (numbers
| paco.filter(filterer)
| paco.map(mapper)
| paco.dropwhile(drop)
| paco.reduce(reducer, initializer=0))
# Run in event loop
number = paco.run(task((1, 2, 3, 4, 5, 6, 7, 8, 9, 10)))
print('Number:', number) # => 36
License
MIT - Tomas Aparicio
History
0.1.4 (2016-11-28)
fix(#24): explicitly pass loop instance to asyncio.wait (b76d128).
0.1.3 (2016-10-27)
feat(#17): add flat_map function.
feat(#18): add pipeline-style operator overloading composition.
0.1.2 (2016-10-25)
fix(setup.py): fix pip installation.
refactor(api): minor refactors in several functions and tests.
0.1.1 (2016-10-24)
refactor(name): use new project name.
0.1.0 (2016-10-23)
First version (beta)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.