如何在 Celery 工作应用程序中捕获 ForkPoolWorker 值(例如 ForkPoolWorker-8)?
芹菜原木:
[2023-08-31 11:53:53,151: WARNING/ForkPoolWorker-8] init a new token
[2023-08-31 11:53:53,819: WARNING/ForkPoolWorker-8] Update mt1234, set status = 1
[2023-08-31 11:53:53,851: WARNING/ForkPoolWorker-8] Sending MT ...
这是我的示例代码
@shared_task(ignore_result=False)
def push_sms(json) -> int:
#May I capture the "ForkPoolWorker-8" here?
return 1
提前非常感谢。
我尝试过使用 celery.signals 但该方法似乎没有被执行
from celery.signals import celeryd_after_setup
@celeryd_after_setup.connect
def capture_worker_name(sender, instance, **kwargs):
print("Try to get worker name")
print('{0}'.format(sender))
我自己找到了答案
from billiard import current_process
...
p = current_process()
print(f"process name is {p.name}")
>> process name is ForkPoolWorker-8