Quick start#
Installation#
# uv
uv add binomic
# pip
pip install binomic
Declare tasks#
Declare tasks in a tasks.py module inside your application package (the
worker discovers every tasks.py module through autodiscover):
# myapp/tasks.py
import time
from binomic.task import task
@task("default")
def example(index: int = 0) -> None:
print(f"[{index}] Current time: {time.time()}")
@task("default", mode="cron", cron="*/5 * * * *") # runs every 5 minutes
def periodic() -> None: ...
@task("default", mode="interval", interval=30) # runs every 30 seconds
def poll() -> None: ...
The first positional argument is the queue name and must match a queue in
the client’s queues configuration. Tasks are registered under the function
name as written (case-sensitive: example, periodic, and poll
above), which is how you refer to a task when submitting it.
Functions in the two periodic modes, cron and interval, take no
arguments — a periodic firing has no call arguments to pass, so declaring
one raises ValueError outright. See
Task modes and scheduling for the full description of all
three modes.
Start and submit#
from binomic.client import Binomic, BinomicConfig, BinomicFactory
from binomic.broker import Message
factory = BinomicFactory(
broker_dsn="redis://localhost:6379/0",
module_name="myapp",
config=BinomicConfig(queues=["default"], workers=2, concurrency=5),
)
binomic: Binomic = factory.create()
# Entering the context starts the master (spawning worker subprocesses)
# and exposes the submission entry point; construction has already
# discovered and registered every tasks.py module inside myapp
async with binomic:
await binomic.submit(Message(task="example", args=[1]))
broker_dsnis the only Redis connection you supply: both delivery and consumption go through the Redis Streams it points at, while the master’s liveness supervision of workers goes over inter-process IPC and needs no Redis;in
BinomicConfig,workersis the number of worker subprocesses the master spawns,concurrencyis the number of tasks each worker runs concurrently, andmax_attemptsis the attempt budget for a single message, whilecapacityis one unacknowledged-total cap shared by every queue (defaultNone, unlimited) — see Reliability for both;task_timeout(total delivery deadline, in seconds),read_countandpoll_interval(the fetch loop), andheartbeat_intervalandreclaim_interval(the liveness beat and crash-recovery cadence) are fields too, with defaults in the API reference;a
Message’staskis the task name andargs/kwargscarry the call arguments (the example above callsexamplewithindex=1). Which stream a message lands in is decided by the queue in the task declaration — a message neither needs nor can specify a queue;enqueued_atneeds no manual assignment: the client-side broker stamps it – not onto theMessageitself – at the moment of actual delivery, which is also why periodic tasks see the current time on every delivery;telemetry is enabled with
otel_enabled, andworker_initinstalls the SDK inside each worker subprocess; see Observability.