Skip to content

Instantly share code, notes, and snippets.

@douglasmiranda
Created April 12, 2024 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save douglasmiranda/bad1dda7074652564047d34805c21a1f to your computer and use it in GitHub Desktop.
Save douglasmiranda/bad1dda7074652564047d34805c21a1f to your computer and use it in GitHub Desktop.
Dramatiq Task Queue - Check task status OR retrieve task result/status

It's much better simply implementing the callbacks on_success / on_failure.

import dramatiq


@dramatiq.actor
def identity(x):
    return x


@dramatiq.actor
def print_result(message_data, result):
    print(f"The result of message {message_data['message_id']} was {result}.")

@dramatiq.actor
def print_error(message_data, exception_data):
    print(f"Message {message_data['message_id']} failed:")
    print(f"  * type: {exception_data['type']}")
    print(f"  * message: {exception_data['message']!r}")


if __name__ == "__main__":
    identity.send_with_options(
        args=(42,),
        on_failure=print_error,
        on_success=print_result,
    )

Literally taken from docs: https://dramatiq.io/cookbook.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment