Skip to main content

Extending Python's process pool to support async functions.

Project description

aio-pool

Extending Python's multiporcessing.Pool to support coroutine functions.
Can be useful for when using a server with very high bandwidth or doing both very large IO and CPU tasks at the same time.

All methods of multiprocessing.Pool are supported.
All paramters for multiprocessing.Pool are supported.

examples:

Setting concurrency limit. This means each process can run with up to 8 concurrent tasks at a time.

import asyncio
from aio_pool import AioPool


async def powlong(a):
  await asyncio.sleep(1)
  return a**2

if __name__ == '__main__':
  with AioPool(processes=2, concurrency_limit=8) as pool:
    results = pool.map(powlong, [i for i in range(16)])  # Should take 2 seconds (2*8).
    print(results) 

Async initliazers are also suppported.

import asyncio
from aio_pool import AioPool

async def start(message):
  await asyncio.sleep(1)
  print(message)

async def powlong(a):
  await asyncio.sleep(1)
  return a**2

if __name__ == '__main__':
  with AioPool(processes=2, 
               concurrency_limit=8, 
               initializer=start,
               init_args=("Started with AioPool", )) as pool:
    results = pool.map(powlong, [i for i in range(16)])  # Should take 2 seconds (2*8).
    print(results) 
    

By default, AioPool also set up a default executor for any non-async tasks.
The size can be determined by threadpool_size arguemnt, which defaults to 1.
None default event loops(uvloop for example) are supported as well, using the loop_initializer argument.
Also, non-async functions are supported by default, as the AioPool worker identify if the function is async or not.
If the function is not async, it runs inside the threadpool, to allow the requested concurrency.
This means that order of execution is not guaranteed, even if the function is not async.
However, the order of results is guaranteed through the pool API (map, starmap, apply, etc...).

from aio_pool import AioPool
import uvloop

with AioPool(loop_initializer=uvloop.new_event_loop, threadpool_size=4) pool:
  pool.map(print, [i for i in range(8)])

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

aio-pool-0.1.2.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

aio_pool-0.1.2-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file aio-pool-0.1.2.tar.gz.

File metadata

  • Download URL: aio-pool-0.1.2.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.9.5 Linux/5.4.0-1047-azure

File hashes

Hashes for aio-pool-0.1.2.tar.gz
Algorithm Hash digest
SHA256 9dd9729bf9f87719a49da11e673e0a4873a0f1ccf55ddb4f6bcc55f966815ec6
MD5 f463498633d883f2d1a07672dfcc701e
BLAKE2b-256 c32b5d3ace55a7ebd359182431fafbd133aa7e3fad616069243d6509218aad2f

See more details on using hashes here.

File details

Details for the file aio_pool-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: aio_pool-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.9.5 Linux/5.4.0-1047-azure

File hashes

Hashes for aio_pool-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ba7cc0c67de9be352df3bd2dcb6fe100e109ebc1e0e5a86e8a4232e007b12029
MD5 286b7639ad6bd931924a6e7a065f081f
BLAKE2b-256 b6e3ce20ca76372f513899d8a3db80984378358bc4c278d5f84393cc1124e83f

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