Skip to main content

buildstatus coverage

Asyncio background tasks

Asyncio background tasks in Python 3.7 and later.

Run CPU intensive long running tasks without blocking the asyncio loop, implemented as a lightweight asyncio layer on top of the multiprocessing module.

Alternatively run tasks in other threads.

Project homepage: https://github.com/eerimoq/asyncbg

Documentation: https://asyncbg.readthedocs.org/en/latest

Installation

pip install asyncbg

Examples

There are more examples in the examples folder.

Call

Call work(a, b) in another process. The script output is Result: 9.

import asyncio
import asyncbg

def work(a, b):
    return a + b

async def main():
    result = await asyncbg.call(work, 4, 5)
    print(f'Result: {result}')

asyncio.run(main())

Process pool

Create a process pool with two workers, and call work() three times in it (up to two callbacks called in parallel).

import asyncio
import asyncbg

def work():
    pass

async def main():
    with asyncbg.ProcessPoolExecutor(max_workers=2) as pool:
        await asyncio.gather(pool.call(work),
                             pool.call(work),
                             pool.call(work))

asyncio.run(main())

Call thread

Call work(a, b) in another thread. The script output is Result: 9.

import asyncio
import asyncbg

def work(a, b):
    return a + b

async def main():
    result = await asyncbg.call_thread(work, 4, 5)
    print(f'Result: {result}')

asyncio.run(main())

Thread pool

Create a thread pool with two workers, and call work() three times in it (up to two callbacks called in parallel).

import asyncio
import asyncbg

def work():
    pass

async def main():
    with asyncbg.ThreadPoolExecutor(max_workers=2) as pool:
        await asyncio.gather(pool.call(work),
                             pool.call(work),
                             pool.call(work))

asyncio.run(main())

Metadata

Release files for asyncbg 0.12.0

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

Source distribution (sdist)

Source distribution for asyncbg 0.12.0
File Size Uploaded
asyncbg-0.12.0.tar.gz 3.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for asyncbg 0.12.0
File Interpreter ABI Platform
asyncbg-0.12.0-py3-none-any.whl Python 3 none any Details

Total release size: 7.4 kB

Release files / asyncbg-0.12.0.tar.gz

Download URL asyncbg-0.12.0.tar.gz
Size 3.9 kB
Tags Source
SHA-256 checksum
How to use checksums
b8dc556953f829e4b2c31307cd7480273ef88322468dc1ef0c7d1c292bf056fc
BLAKE2b-256 checksum
How to use checksums
fec4905e83237e0357fc86637a2099adddb75d34d4780166e82088e49e1295f6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / asyncbg-0.12.0-py3-none-any.whl

Download URL asyncbg-0.12.0-py3-none-any.whl
Size 3.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
12bc0046830a52b50a67a98a927c1dd36c2aea978baf0cda2d556eae39cfcb39
BLAKE2b-256 checksum
How to use checksums
8935dcac913f52ca4da452f3c15b2095da2745deff832c568eb9fc06305f94d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release history Release notifications | RSS feed

This release

0.12.0 This release

2 release files

0.11.0

2 release files

0.9.0

1 release file

0.8.0

1 release file

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

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