Async flow made easy and fun
Project description
Features
Utilities to describe and execute flow with coroutine functions
Easily launch several flows simultaneously
Strong focus on readability
Requirements
python 3.7 +
This library is easier to use with third party libraries for manipulating function such as fn (flip function, function composition…), and tenacity (retry library).
Installation
You can install Aflowey via pip from PyPI:
$ pip install aflowey
Usage
Chain function to execute an async flow !
from aflowey import aflow, CANCEL_FLOW, aexec, flog, partial
db = ... # get client db
# add some other library
from tenacity import retry
async def fetch_url(url):
return await ...
def process_data(html):
processed_data = ... # process data
if processed_data is None:
return CANCEL_FLOW
return processed_data
async def insert_into_db(content):
return await db.insert_one(content)
def get_url_flow(url):
# defining a flow for working with url
return (
aflow.from_args("http://goole.fr")
>> retry(wait=2)(fetch_url)
>> flog("url fetched", print_arg=True)
>> process_data # this one is synchronous but may included in the flow
>> insert_into_db
)
Execute the flow for one url:
result = await get_url_flow("http://google.com/...").run()
Execute several flows asynchronously:
from fn import flip
name = "Marco"
user_flow = (
aflow.empty()
>> partial(db.find_one, search={"username": name})
>> User.from_dict
# the impure indicate that this step does not return a new result
# i.e the result of User.from_dict will be sended
>> impure(partial(flip(setattr), datetime.now(), 'created_at'))
)
organization_id = "Not employed"
organization_flow = (
aflow.empty()
>> partial(db_find_one, search={"organization_id": organization_id})
>> Organization.from_dict
)
urls = [
"http://google.com/...",
"http://google.com/...",
"http://google.com/...",
"http://google.com/...",
]
url_flows = [get_url_flow(url) for url in urls]
# execute all flow with asyncio gather method
executor = aexec().from_flows(url_flows) | user_flow | organization_flow
(url1, url2, url3, url4), user, organization = await executor.run()
It can be boring to create function that exactly matches arity of the flow. Aflowey provide some higher order functions to help, see:
lift: create a new method accepting transformed arguments
F0: from a 0 argument function, create one argument function to fit the arity of the flow
F1: create a new function with an extra parameter to process input of the flow step
spread: create a new function which spread an iterable of arguments into the given function
spread_kw: create a new function which spread kw arguments into the given function
The fn library provide other interesting functions like:
flip
first
If you have any other ideas…
Contributing
Contributions are very welcome. To learn more, see the Contributor Guide.
License
Distributed under the terms of the MIT license, Aflowey is free and open source software.
Issues
If you encounter any problems, please file an issue along with a detailed description.
Credits
This project was generated from @cjolowicz’s Hypermodern Python Cookiecutter template.
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.
Source Distribution
Built Distribution
File details
Details for the file aflowey-0.1.10.tar.gz
.
File metadata
- Download URL: aflowey-0.1.10.tar.gz
- Upload date:
- Size: 13.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2b11a25329e7edf57b23095e2213a2a5b3889f51a7081a90104f197d1b3ccd76 |
|
MD5 | f4e2a39be67d029c0cacbe679b2b7623 |
|
BLAKE2b-256 | 9dfdd5e823920713ae5f9a4c1d325fd4a98fc0a995c41c8d1b7ff14103c4a152 |
File details
Details for the file aflowey-0.1.10-py3-none-any.whl
.
File metadata
- Download URL: aflowey-0.1.10-py3-none-any.whl
- Upload date:
- Size: 13.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e26f3c9489cefd849857d5607dd6bc58d24f009666945b5ed4059b6513d46b37 |
|
MD5 | 9d527d28b5c45a6dc9a742eeca1e20c6 |
|
BLAKE2b-256 | 3c2fde47be7ae925e5f9151ca6ff1ac11eff65dbe270085c05b47b893357dd51 |