Skip to main content

Asyncio background tasks.

Project description

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())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asyncbg-0.12.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

asyncbg-0.12.0-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file asyncbg-0.12.0.tar.gz.

File metadata

  • Download URL: asyncbg-0.12.0.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for asyncbg-0.12.0.tar.gz
Algorithm Hash digest
SHA256 b8dc556953f829e4b2c31307cd7480273ef88322468dc1ef0c7d1c292bf056fc
MD5 50848321cdee9e65b60150e46f82e084
BLAKE2b-256 fec4905e83237e0357fc86637a2099adddb75d34d4780166e82088e49e1295f6

See more details on using hashes here.

File details

Details for the file asyncbg-0.12.0-py3-none-any.whl.

File metadata

  • Download URL: asyncbg-0.12.0-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for asyncbg-0.12.0-py3-none-any.whl
Algorithm Hash digest
SHA256 12bc0046830a52b50a67a98a927c1dd36c2aea978baf0cda2d556eae39cfcb39
MD5 abc200738904c66dfa3de987db249fbb
BLAKE2b-256 8935dcac913f52ca4da452f3c15b2095da2745deff832c568eb9fc06305f94d6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page