Generic worker that performs concurrent tasks using coroutine.
Project description
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)
Links & Contact Info
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file coworker-2.0.1.tar.gz.
File metadata
- Download URL: coworker-2.0.1.tar.gz
- Upload date:
- Size: 14.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1bdf9b2c2feb368715434524ac5fda8d518fbff3b8827c7ee31c47eee46668f7
|
|
| MD5 |
aa27871befe81cc46b9f234f6ca96a48
|
|
| BLAKE2b-256 |
05cd0bbed485ed06e1c82450a51a29d45926531a8b3e8d5e6838ed0b873cbd49
|
File details
Details for the file coworker-2.0.1-py3-none-any.whl.
File metadata
- Download URL: coworker-2.0.1-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
51550ae1003b68b2e41ccbffbbdb058bda4b0b7734c3466adfda3e70999a77ea
|
|
| MD5 |
1070b4559c5497637f8b8dd2a04b321d
|
|
| BLAKE2b-256 |
65537fd132410d8914154e10f41c89c285c4dadf408ca5e67a138387e90c04e1
|