Search code examples
dramatiq

Dramatiq: Getting the worker name within the actor function


Does Dramatiq offer a way to get some sort of a human-readable name of the worker within the @dramatiq.actor function? I would use this to separate log lines from different works.

For example, something like dramatiq-process-1-worker-3.

@dramatiq.actor(broker=redis_broker, store_results=True, result_ttl=10*1000)
def ping():
     # Log worker name here

Solution

    • you can use the CurrentMessage middllware
    from dramatiq.middleware import CurrentMessage
    
    @dramatiq.actor(broker=redis_broker, store_results=True, result_ttl=10*1000)
    def ping():
        msg = CurrentMessage.get_current_message()
        print(msg.actor_name)