Skip to main content

Run a coroutine with each item in an iterable, concurrently

Project description

aioconcurrency

Run a coroutine with each item in an iterable, concurrently

Install

pip install aioconcurrency

Usage example

import aioconcurrency

items = [1, 2, 3, 4]
async def f(item): return item * 2

await aioconcurrency.map(items, f, concurrency=2).results()  # Returns [2, 4, 6, 8]

async for result in aioconcurrency.each(items, f, concurrency=2):
    print(result)  # Prints 2 4 6 8 in random order

Api

aioconcurrency.map

Runs the given coroutine concurrently with each item in an iterable. The list of the return values will be ordered as if the coroutine was ran serially.

items

An iterable object.

coro

Coroutine to feed each item to.

optional: concurrency

Number of concurrent runs of coro. Defaults to aioconcurrency.Infinite.

property: results()

Coroutine. Must be awaited to obtain the list of return values.

property: processed_count

The number of items that have been processed so far.

property: cancel()

Cancels all runs of coro.

aioconcurrency.each

Runs the given coroutine concurrently with each item in an iterable. Returns a generator that may be used to iterate over the return values. The generator yields values as soon as they are available.

items

An iterable object. If an asyncio.Queue is passed then .each will read from it indefinitely.

coro

Coroutine to feed each item to.

optional: concurrency

Number of concurrent runs of coro. Defaults to aioconcurrency.Infinite.

optional: discard_results

If truthy, discard the return value of coro. Defaults to false.

property: wait()

Coroutine. May be used to wait until all items have been processed.

property: processed_count

The number of items that have been processed so far.

property: cancel()

Cancels all runs of coro.

Tests

pytest .

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

aioconcurrency-0.1.3.tar.gz (3.5 kB view details)

Uploaded Source

File details

Details for the file aioconcurrency-0.1.3.tar.gz.

File metadata

  • Download URL: aioconcurrency-0.1.3.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.18.1 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.29.1 CPython/3.7.2

File hashes

Hashes for aioconcurrency-0.1.3.tar.gz
Algorithm Hash digest
SHA256 3c627ce7d8524d8805bd4d09f851eff2c47595d73cf4ac9110b103aeb1e8db71
MD5 7eef98d6e70980e805674535d1edfe35
BLAKE2b-256 5853eded7bfaf2a0e3953fa352bb31df6028b35f78feba3771c18706d4b97d7b

See more details on using hashes here.

Provenance

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