How to get the processed results from dramatiq python?

2.4k Views Asked by At
import dramatiq
from dramatiq.brokers.redis import RedisBroker
from dramatiq.results import Results
from dramatiq.results.backends import RedisBackend

broker = RedisBroker(host="127.0.0.1", port=6379)
broker.declare_queue("default")
dramatiq.set_broker(broker)
# backend = RedisBackend()
# broker.add_middleware(Results(backend=backend))

@dramatiq.actor()
def print_words(text):
        print('This is ' + text)

print_words('sync')
a = print_words.send('async')
a.get_results()

I was checking alternatives to celery and found Dramatiq. I'm just getting started with dramatiq and I'm unable to retrieve results. I even tried setting the backend and 'save_results' to True. I'm always getting this AttributeError: 'Message' object has no attribute 'get_results'

Any idea on how to get the result?

2

There are 2 best solutions below

3
Bogdan Popa On

You were on the right track with adding a result backend. The way to instruct an actor to store results is store_results=True, not save_results and the method to retrieve results is get_result(), not get_results.

0
David Morre On

When you run get_result() with block=False, you should wait the worker set result ready, like this:

while True:
    try:
        res = a.get_result(backend=backend)
        break
    except dramatiq.results.errors.ResultMissing:
        # do something like retry N times.
        time.sleep(1)
print(res)