Skip to main content

coworker

Generic worker that performs concurrent tasks using coroutine.

Quick Start Tutorial

Define how a task is performed – do_task takes a single input and optionally returns a result:

from coworker import Coworker

class SquareWorker(Coworker):
    async def do_task(self, task):  # Task can be anything, such as a tuple with a callable and args.
        return task * task

Now let’s do some work:

import asyncio

async def background_worker_example():
    worker = SquareWorker(max_concurrency=5)    # Only 5 tasks will run concurrently. Defalts to 10

    # Single task
    result = await worker.do(2)
    print(result)   # result = 4

    # Mulitiple tasks -- get all results after everything is done
    tasks = list(range(100))
    results = await worker.do(tasks)
    print(results)  # results = [0, 1, 4, 9, ...]

    # Mulitiple tasks -- get first completed
    tasks = list(range(10))
    for result in worker.do(tasks, as_iterator=True):
        print(await result)  # results = 0, 1, 4, 9, ...

# Run async usage example
asyncio.run(background_worker_example())

Besides max_concurrency, you can also pass the following params to Coworker:

  • sliding_window to use sliding window or tumbling window when processing the tasks concurrently. Defaults to True.

  • do_task for a callable to do the task instead of having to sub-class. E.g. Coworker(do_task=lamdba x: x * x)

Release files for coworker 2.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 coworker 2.0.1
File Size Uploaded
coworker-2.0.1.tar.gz 14.0 kB Details

Built distribution (wheel)

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

Total release size: 19.5 kB

Release files / coworker-2.0.1.tar.gz

Download URL coworker-2.0.1.tar.gz
Size 14.0 kB
Tags Source
SHA-256 checksum
How to use checksums
1bdf9b2c2feb368715434524ac5fda8d518fbff3b8827c7ee31c47eee46668f7
BLAKE2b-256 checksum
How to use checksums
05cd0bbed485ed06e1c82450a51a29d45926531a8b3e8d5e6838ed0b873cbd49
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release files / coworker-2.0.1-py3-none-any.whl

Download URL coworker-2.0.1-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
51550ae1003b68b2e41ccbffbbdb058bda4b0b7734c3466adfda3e70999a77ea
BLAKE2b-256 checksum
How to use checksums
65537fd132410d8914154e10f41c89c285c4dadf408ca5e67a138387e90c04e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release history Release notifications | RSS feed

This release

2.0.1 This release

2 release files

2.0.0

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

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