Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

workraft-0.4.2.tar.gz (42.1 kB view details)

Uploaded Source

Built Distribution

workraft-0.4.2-py3-none-any.whl (20.9 kB view details)

Uploaded Python 3

File details

Details for the file workraft-0.4.2.tar.gz.

File metadata

  • Download URL: workraft-0.4.2.tar.gz
  • Upload date:
  • Size: 42.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.27.0

File hashes

Hashes for workraft-0.4.2.tar.gz
Algorithm Hash digest
SHA256 13ccf4254244e6a08e510a16d9b1a36e5be31c7bc2c929f4adad3311f7e43e8a
MD5 c4eec26e3a83777372879037aea22e8d
BLAKE2b-256 be9c8bc4a1121474cc9cf603d4cbd38555bf2d20104ba4754d893e4bdc03e7c7

See more details on using hashes here.

File details

Details for the file workraft-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: workraft-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 20.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.27.0

File hashes

Hashes for workraft-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4a46902cd71e8ad982a15bd26ad65716ccf4f52a6c728fe08c227208c6278c2d
MD5 3c31b92d58f6a9d1994f433e5db41b9c
BLAKE2b-256 6f2dc31e860d64f54adb45102c58e8dfc4d955e91fb23ec9a4729b24b57f2689

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