Step 6: long tracks in the background
Step 6 of 8
Run slow work as a job and wait for it.
A 30-second track returns quickly, but a long one can take minutes. Holding a connection open that long is fragile. Run it as a background job instead: the call returns a Job at once, and you wait for it.
job = catalyst.composer.compose(prompt, effort="low", duration=120, async_=True)
env = job.wait(timeout=600, on_progress=lambda e: print("music is", e.status))
master = next(f for f in env.files if f.role == "master")wait long-polls, so it does not hammer the API. If the job fails, wait raises the job's own error, and if you run out of time it raises a timeout error while the job keeps running on the server. The files in the result carry their session, so download(master) works unchanged.
Prefer to be called back? Use a webhook: see Background jobs and webhooks.