Skip to main content

Aplex

build coverage platform supported pythons package version license maintenance

Translation: 简体中文 | 繁體中文

Aplex is a Python library for combining asyncio with multiprocessing and threading.

  • Aplex helps you run coroutines and functions in other process or thread with asyncio.
  • Aplex provides a usage like that of standard library concurrent.futures, which is familiar to you and intuitive.
  • Aplex lets you do load balancing in a simple way if you need.

Installation

For general users, use the package manager pip to install aplex.

pip install aplex

For contributors, install with pipenv:

git clone https://github.com/lunluen/aplex.git
cd aplex
pipenv install --dev

or with setuptools

git clone https://github.com/lunluen/aplex.git
cd aplex
python setup.py develop

Usage

Definition to know:

A work is a callable you want to run with asyncio and multiprocessing or threading. It can be a coroutine function or just a function.

In below case, the work is the coroutine function demo.

Submit

You can submit your work like:

import aiohttp
from aplex import ProcessAsyncPoolExecutor

async def demo(url):
    async with aiohttp.request('GET', url) as response:
        return response.status

if __name__ == '__main__':
    pool = ProcessAsyncPoolExecutor(pool_size=8)
    future = pool.submit(demo, 'http://httpbin.org')
    print('Status: %d.' % future.result())

Note: If you are running python on windows, if __name__ == '__main__': is necessary. That's the design of multiprocessing.

Result:

Status: 200

Map

For multiple works, try map:

iterable = ('http://httpbin.org' for __ in range(10))
for status in pool.map(demo, iterable, timeout=10):
    print('Status: %d.' % status)

Awaiting results

Aplex allows one to await results with loop that already exists. It's quite simple.

Just set keyword argument awaitable to True!

For example:

pool = ProcessAsyncPoolExecutor(awaitable=True)

Then

future = pool.submit(demo, 'http://httpbin.org')
status = await future

How about map?

async for status in pool.map(demo, iterable, timeout=10):
    print('Status: %d.' % status)

Load balancing

In aplex, each worker is the process or thread on your computer. That is, they have the same capability computing. But, your works might have different workloads. Then you need a load balancer.

Aplex provides some useful load balancers. They are RoundRobin, Random, and Average. The default is RoundRobin.

Simply set this in contruction keyword argument:

from aplex.load_balancers import Average

if __name__ == '__main__':
    pool = ProcessAsyncPoolExecutor(load_balancer=Average)

Done. So easy. 💯

You can also customize one:

from aplex import LoadBalancer

class MyAwesomeLoadBalancer(LoadBalancer):
    def __init__(*args, **kwargs):
        super().__init__(*args, **kwargs)  # Don't forget this.
        awesome_attribute = 'Hello Aplex!'

    def get_proper_worker(self):
        the_poor_guy = self.workers[0]
        return the_poor_guy

See details of how to implement a load balancer at:

Worker loop factory

By the way, if you think the build-in asyncio loop is too slow:

import uvloop

if __name__ == '__main__':
    pool = ProcessAsyncPoolExecutor(worker_loop_factory=uvloop.Loop)

Like this?

Scroll up and click Watch - Releases only and Star as a thumbs up! 👍

Any feedback?

Feel free to open a issue (just don't abuse it).

Or contact me: mas581301@gmail.com 📫

Anything about aplex is welcome, such like bugs, system design, variable naming, even English grammer of docstrings!

How to contribute

Contribution are welcome.

Asking and advising are also kinds of contribution.

Please see CONTRIBUTING.md

License

MIT

Release files for aplex 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aplex 1.0.1
File Size Uploaded
aplex-1.0.1.tar.gz 500.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aplex 1.0.1
File Interpreter ABI Platform
aplex-1.0.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 535.8 kB

Release files / aplex-1.0.1.tar.gz

Download URL aplex-1.0.1.tar.gz
Size 500.7 kB
Tags Source
SHA-256 checksum
How to use checksums
e393fabe502f827fcc02b7dda68489c9b8562ed10d38a58089f73b54fbef5ea9
BLAKE2b-256 checksum
How to use checksums
cb979af8e96a68ba698c86f5a808cd8ce50b64380d015a84ac705fc0ea088aa3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.0 CPython/3.7.1

Release files / aplex-1.0.1-py2.py3-none-any.whl

Download URL aplex-1.0.1-py2.py3-none-any.whl
Size 35.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
1f9e12bb3802703d475776055ded237cb64f69ccf96e496fa64374c0ad6eafab
BLAKE2b-256 checksum
How to use checksums
ef67fc4f21422741b5cb9511f2a95d45290d8a15326c8184cfb640c47b3482dd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.0 CPython/3.7.1

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page