python threading.local调用问题

a.py里创建了thread_local = threading.local()
然后a.py调用b.py或c.py的函数。
b.py或c.py,不通过传参数,怎么输出thread_local值?
代码如下:
a.py

import threading
from concurrent.futures import ThreadPoolExecutor

from b import print_txt


def task():
    thread_local.test = 'test'
    print_txt()


thread_local = threading.local()
max_workers = 2
with ThreadPoolExecutor(max_workers=max_workers, ) as executor:
    for i in range(2):
        future = executor.submit(task)

b.py

def print_txt():
    # 怎么输出thread_local.test的值
    pass
阅读 1.4k
1 个回答

在b.py里给print_txt函数加个参数,在里面用print打印这个参数。
然后在a.py的task里print_txt(thread_local.test)

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题