我知道在python中使用threadlocal是可能的,但是由于某些原因,我无法找到实现这一点的确切语法。我有下面的示例代码来测试这一点,但这不起作用-
模块1.py
import threading
def print_value():
local = threading.local() // what should I put here? this is actually creating a new thread local instead of returning a thread local created in main() method of module2.
print local.name;
模数2.py
^{pr2}$共享数据应该只对调用这些函数的线程可用,而不是对系统中的所有线程可用。一个例子是web应用程序中的请求id。在
在模块1中,定义一个全局变量
threading.local
模块1
模块2
^{pr2}$如果它在同一个进程中,为什么不使用singleton?在
现在只需导入Bucket并绑定一些数据
^{pr2}$相关问题 更多 >
编程相关推荐