【发布时间】:2018-12-13 15:02:15
【问题描述】:
我正在尝试将 threading.local() 中的数据传递给 不同模块 中的函数。 代码是这样的:
other_module.py:
import threading
# 2.1
ll = threading.local()
def other_fn():
# 2.2
ll = threading.local()
v = getattr(ll, "v", None)
print(v)
main_module.py:
import threading
import other_module
# 1.1
ll = threading.local()
def main_fn(v):
# 1.2
ll = threading.local()
ll.v = v
other_fn()
for i in [1,2]:
t = threading.Thread(target=main_fn, args=(i,))
t.start()
但没有一个组合 1.x - 2.x 不适合我。 我发现了类似的问题 - Access thread local object in different module - Python 但如果 print_message 函数位于不同的模块中,则回复,标记为答案对我也不起作用。
是否可以在模块之间传递线程本地数据而不将其作为函数参数传递?
【问题讨论】:
-
你找到答案了吗?
-
@ShivanshJagga 不,我没有。我希望它使用操作系统本机 TLS,但事实并非如此。所以我最终将我需要的所有数据作为线程函数参数传递。
标签: python multithreading module local