A simple worker library in Python
Project description
Workraft
Description
Workraft is a lightweight, highly opionated, simple worker library in Python with only a Postgres database as the single source of truth; no Redis, RabbitMQ, or any other external dependencies.
Installation
Run the command
pip install workraft
Usage
First, you need to setup the database. The following environment variables are required:
- WK_DB_HOST - The database host
- WK_DB_PORT - The database port
- WK_DB_USER - The database user
- WK_DB_PASS - The database password
- WK_DB_NAME - The database name
Then, you need to setup the database using the command below:
python3 -m workraft build_stronghold
To create a worker, you need to give it some tasks to perform. For example:
import time
from workraft.core import Workraft
workraft = Workraft()
@workraft.task("simple_task")
def simple_task(a: int, b: int, c: int) -> int:
time.sleep(1)
# time.sleep(random.randint(10, 20))
raise ValueError("Random error!")
return a + b + c
To run a worker, run the command below:
python3 -m workraft peon --workraft_path=example.workraft
To send a task to the worker, you can use the following example:
async def main():
a = random.randint(1, 100)
b = random.randint(1, 100)
c = random.randint(1, 100)
await workraft.send_task_async(
"simple_task",
get_db_config(),
[a, b],
task_kwargs={"c": c},
retry_on_failure=True,
)
if __name__ == "__main__":
asyncio.run(main())
And that's it. Use any database viewer to see the tasks and their status.
License
MIT
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
File details
Details for the file workraft-0.4.1.tar.gz
.
File metadata
- Download URL: workraft-0.4.1.tar.gz
- Upload date:
- Size: 42.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.27.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1f71fda1a5e889f1db3db0fa0f5941112d7cb1b99001a0072d17ca02faf544d8 |
|
MD5 | 8ddf19b7be5ee161a24955ba328f92c0 |
|
BLAKE2b-256 | 81a2d72bb214b4bc856f946257cbff195d132fdc728e70065a629a03415b172f |
File details
Details for the file workraft-0.4.1-py3-none-any.whl
.
File metadata
- Download URL: workraft-0.4.1-py3-none-any.whl
- Upload date:
- Size: 20.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.27.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 188d85c9257d69e34d7ca48e2f3a23b293960f5a8f55829e81c0b922bb4d9738 |
|
MD5 | d238f6e25f304e14999fc0ed70fd8cf0 |
|
BLAKE2b-256 | f58695112c96b1b0e514a07b06572676ac14221424a912f6e7e8ccd48abf0fb6 |