python threading.local调用问题

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


回答:

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

以上是 python threading.local调用问题 的全部内容, 来源链接: utcz.com/p/937973.html

回到顶部