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 hyfrom pydantic import BaseModel, Fieldhy = Hyrex( app_id="kindex-worker", conn=settings.hy_database_url, error_callback=task_error_callback,)classDeliverEmailContext(BaseModel): email:str subject:str body:str@hy.taskdefdeliver_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, DeliverEmailContextemail_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!