Is it necessary to pass a shared variable explicitly设置为如下所示的线程函数(第一个示例),如下面的代码所示
操作系统为Windows 10,Python解释器为CPython
或
从第二个代码示例所示的函数是否可以设置为directly access the shared variable.
# First example
# passing Arguments to the threading function using args
imp或t time
imp或t threading
def update_list_A(var_list):
var_list.append('A')
shared_list =[]
t1 = threading.Thread(target = update_list_A, args = (shared_list,)) #usings args to send shared variable
t1.start()
t1.join()
print(shared_list)
+
#second example
#directly accessing the shared variable from the threading function
imp或t time
imp或t threading
def update_list_A():
shared_list.append('A')
shared_list =[] #shared List
t1 = threading.Thread(target = update_list_A, )
t1.start()
t1.join()
print(shared_list)
What is the c或rect way to access the shared variable, is it through args 或 directly?