我一直在嘗試使用列表來存儲每個線程的所有返回值。線程函數返回一個包含三個連續數字的列表。 rt_list必須是列表的列表,其中每個項目是每個線程的輸出列表。在Python中使用列表的線程
from threading import Thread
def thread_func(rt_dict, number = None):
if not(number):
print("Number not defined.Please check the program invocation. The program will exit.")
sys.exit()
else:
rt_dict[number] = [number,number+1, number+2]
return
numbers = [1,2,3,4,5,6]
rt_list = []
thread_list = [Thread(target=thread_func, args=(rt_list),kwargs={'number':num})for num in numbers]
for thread in thread_list:
thread.start()
for thread in thread_list:
thread.join()
print(rt_list)
這是錯誤我得到當我試圖運行上述程序
Exception in thread Thread-6:
Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/lib/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
TypeError: thread_func() missing 1 required positional argument: 'rt_dict'
你通過一個列表或字典嗎? –
你可以嘗試'線程(target = thread_func,kwargs = {'number':num,'rtdict':rtlist})'(無位置,只是關鍵字)。但是仍然有列表/字典問題 –