我正在关注asyncio.Queue. 在由worker_task()我调用的函数中,我想记录当前的工作人员姓名,而不必将其传递到任何地方。
我经历的某种组合怀疑LoggerAdapter,并ContextVar可以做我想做的。
以下内容...
import logging
logging.basicConfig(format="%(worker_name)s %(message)s")
logger = logging.getLogger(__name__)
async def sub_function():
logger.info("a message") # worker-1: a message
async def worker_task(queue, worker_name):
logger.do_something(worker_name) # HELP HERE PLEASE
await sub_function()
queue = asyncio.Queue()
workers = [
loop.create_task(worker_task(queue, "worker-1"),
loop.create_task(worker_task(queue, "worker-2"),
]
心有法竹
相关分类