主要知识点:
- queue 队列
- get,put 方法 每次取一个、添加一个
队列其实就是按照顺序执行,达到自已的结果预期。
就想排队买东西一样,
from queue import Queue
worker = Queue()
resulter = Queue()
worker.put(job)
resulter.put()
resulter.get() #每次取一个
全部代码
import threading
import time
from queue import Queue
def worker(list,q):
for item in range(len(list)):
list[item] = list[item]**2
q.put(list)
def main():
q = Queue()
tsList = []
data = [[33,23,43],[1,3,4],[55,222,343],[656,3434,2323]]
for i in range(4):
t = threading.Thread(target=worker, args=(data[i], q))
t.start()
tsList.append(t)
for item in tsList:
item.join()
results = []
for x in range(4):
results.append(q.get())
print(results)
return results
if __name__ == '__main__':
main()