Dramatiq:在演员功能中获取工人姓名

Dramatiq: Getting the worker name within the actor function

Dramatiq 是否提供了一种方法来在 @dramatiq.actor 函数中获取某种人类可读的工人名称?我会用它来将日志行与不同的作品分开。

例如 dramatiq-process-1-worker-3.

@dramatiq.actor(broker=redis_broker, store_results=True, result_ttl=10*1000)
def ping():
     # Log worker name here
  • 您可以使用 CurrentMessage 中间件
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)