My First Hyrex Task

Step 1. Install Hyrex in your project:

pip install hyrex

Step 2. Initialize your postgres database the Hyrex CLI. Note that the database URL should be a Postgres connection string.

hyrex init --database_url="ADD_URL_HERE"

Step 3. Initialize the Hyrex object in your project and decorate a task.

from settings import hy
from pydantic import BaseModel, Field

hy = Hyrex(
  app_id="kindex-worker",
  conn=settings.hy_database_url,
  error_callback=task_error_callback,
)

class DeliverEmailContext(BaseModel):
    email: str
    subject: str
    body: str

@hy.task
def deliver_email(context: DeliverEmailContext):
    url = "EMAIL_PROVIDER_API"
    r = requests.post(url, body=context.model_dump())
    if r.status_code == 200:
        print("Success!")
    else:
        print("Something went wrong...")

Step 4: Send the task to be executed asynchronously. Here deliver_email is a hyrex task

from tasks import deliver_email, DeliverEmailContext

email_args = SendEmailContext(
  email="bart@hyrex.io"
  subject="Breakfast is ready!"
  body="like I said... breakfast is ready!"
)

# Call the function synchronously.
# Behaves just like a normal Python function.
deliver_email(email_args)

# Use .send(...) to call the function asynchronously.
# Submits the task to the Hyrex task queue.
task = deliver_email.send(email_args)

There you go! You now have a distributed task queue!