参考链接: 【莫烦Python】Threading 学会多线程 Python
参考链接: 莫烦 多线程
参考链接: threading — 基于线程的并行
参考链接: queue — 一个同步的队列类
testThreading4_1.py
# 教学视频第4集
import threading
import time
from queue import Queue
def job(l,q):
for i in range (len(l)):
l[i] = l[i]**2
q.put(l)
def multithreading():
q =Queue()
threads = []
data = [[1,2,3],[3,4,5],[4,4,4],[5,5,5]]
for i in range(4):
t = threading.Thread(target=job,args=(data[i],q))
t.start()
threads.append(t)
for thread in threads:
# thread.start()
thread.join()
results = []
for _ in range(4):
results.append(q.get())
print(results)
if __name__=='__main__':
multithreading()
r'''
输出结果:
[[1, 4, 9], [9, 16, 25], [16, 16, 16], [25, 25, 25]]
'''
debug下观察中间结果:
testThreading4_1_2.py
# 教学视频第4集
import threading
import time
from queue import Queue
def job(l,q):
for i in range (len(l)):
l[i] = l[i]**2
time.sleep(1.1)
q.put(l)
def multithreading():
q =Queue()
threads = []
data = [[1,2,3],[3,4,5],[4,4,4],[5,5,5]]
for i in range(4):
t = threading.Thread(target=job,args=(data[i],q))
t.start()
threads.append(t)
for thread in threads:
# thread.start()
thread.join()
results = []
for _ in range(4):
results.append(q.get())
print(results)
if __name__=='__main__':
multithreading()
r'''
输出结果:
[[1, 4, 9], [9, 16, 25], [16, 16, 16], [25, 25, 25]]
'''
注意输出结果顺序的不确定性:
Windows PowerShell
尝试新的跨平台 PowerShell https://aka.ms/pscore6
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多线程> & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2021.1.502429796\pythonFiles\lib\python\debugpy\launcher' '57636' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\测试多线程\testThreading4_1.py'
[[1, 4, 9], [9, 16, 25], [16, 16, 16], [25, 25, 25]]
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多线程> c:; cd 'c:\Users\chenxuqi\Desktop\News4cxq\测试多线程'; & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2021.1.502429796\pythonFiles\lib\python\debugpy\launcher' '57642' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\测试多线程\testThreading4_1.py'
[[9, 16, 25], [1, 4, 9], [16, 16, 16], [25, 25, 25]]
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多线程> c:; cd 'c:\Users\chenxuqi\Desktop\News4cxq\测试多线程'; & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2021.1.502429796\pythonFiles\lib\python\debugpy\launcher' '57646' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\测试多线程\testThreading4_1.py'
[[9, 16, 25], [1, 4, 9], [16, 16, 16], [25, 25, 25]]
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多线程>
testThreading4_2.py
# 教学视频第4集
import threading
import time
from queue import Queue
def job(l,q):
for i in range (len(l)):
l[i] = l[i]**2
q.put(l)
def multithreading():
q =Queue()
threads = []
data = [[1,2,3],[3,4,5],[4,4,4],[5,5,5]]
for i in range(4):
t = threading.Thread(target=job,args=(data[i],q))
# t.start()
threads.append(t)
for thread in threads:
thread.start()
thread.join()
results = []
for _ in range(4):
results.append(q.get())
print(results)
if __name__=='__main__':
multithreading()
r'''
输出结果:
[[1, 4, 9], [9, 16, 25], [16, 16, 16], [25, 25, 25]]
'''
debug下观察中间结果:
testThreading4_3.py(不使用queue.Queue,而是只使用列表):
# 教学视频第4集
import threading
import time
# from queue import Queue
def job(data,i,q):
l = data[i]
for i in range (len(l)):
l[i] = l[i]**2
time.sleep(1.0)
q.append(l)
def multithreading():
# q =Queue()
q = list()
threads = []
data = [[1.2,2,3],[3,4,5],[4,4,4],[5,5,5]]
for i in range(4):
t = threading.Thread(target=job,args=(data,i,q))
t.start()
threads.append(t)
for thread in threads:
# thread.start()
thread.join()
results = []
for _ in range(4):
results.append(q[_])
print(results)
if __name__=='__main__':
multithreading()
r'''
输出结果:
[[16, 16, 16], [1.44, 4, 9], [9, 16, 25], [25, 25, 25]]
'''
控制台下输出,注意列表输出顺序会打乱.
debug下观察中间结果:
注意结果的不确定性:testThreading4_1_2.py
# 教学视频第4集
import threading
import time
from queue import Queue
def job(l,q):
for i in range (len(l)):
l[i] = l[i]**2
time.sleep(1.1)
q.put(l)
def multithreading():
q =Queue()
threads = []
data = [[1,2,3],[3,4,5],[4,4,4],[5,5,5]]
for i in range(4):
t = threading.Thread(target=job,args=(data[i],q))
t.start()
threads.append(t)
for thread in threads:
# thread.start()
thread.join()
results = []
for _ in range(4):
results.append(q.get())
print(results)
if __name__=='__main__':
multithreading()
r'''
输出结果:
[[16, 16, 16], [9, 16, 25], [1, 4, 9], [25, 25, 25]]
'''
控制台下输出:
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
尝试新的跨平台 PowerShell https://aka.ms/pscore6
加载个人及系统配置文件用了 823 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多线程> & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2021.1.502429796\pythonFiles\lib\python\debugpy\launcher' '58627' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\测试多线程\testThreading4_1.py'
[[16, 16, 16], [9, 16, 25], [1, 4, 9], [25, 25, 25]]
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多线程> c:; cd 'c:\Users\chenxuqi\Desktop\News4cxq\测试多线程'; & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2021.1.502429796\pythonFiles\lib\python\debugpy\launcher' '58764' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\测试多线程\testThreading4_1.py'
[[1, 4, 9], [16, 16, 16], [25, 25, 25], [9, 16, 25]]
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多线程> c:; cd 'c:\Users\chenxuqi\Desktop\News4cxq\测试多线程'; & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2021.1.502429796\pythonFiles\lib\python\debugpy\launcher' '58768' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\测试多线程\testThreading4_1.py'
[[16, 16, 16], [1, 4, 9], [9, 16, 25], [25, 25, 25]]
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多线程>
为了使得最后输出结果不打乱, 采用列表, testThreading4_4.py
# 教学视频第4集
import threading
import time
# from queue import Queue
def job(data,i,q):
l = data[i]
for k in range (len(l)):
l[k] = l[k]**2
time.sleep(1.0)
q[i] = l
def multithreading():
# q =Queue()
q = [None for i in range(4)]
threads = []
data = [[1.2,2,3],[3,4,5],[4,4,4],[5,5,5]]
for i in range(4):
t = threading.Thread(target=job,args=(data,i,q))
t.start()
threads.append(t)
for thread in threads:
# thread.start()
thread.join()
results = []
for i in range(4):
results.append(q[i])
print(results)
if __name__=='__main__':
multithreading()
r'''
输出结果:
[[1.44, 4, 9], [25, 25, 25], [16, 16, 16], [9, 16, 25]]
'''
控制台输出结果:
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
尝试新的跨平台 PowerShell https://aka.ms/pscore6
加载个人及系统配置文件用了 911 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多线程> & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2021.1.502429796\pythonFiles\lib\python\debugpy\launcher' '58980' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\测试多线程\testThreading4_4.py'
[[1.44, 4, 9], [9, 16, 25], [16, 16, 16], [25, 25, 25]]
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多线程> c:; cd 'c:\Users\chenxuqi\Desktop\News4cxq\测试多线程'; & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2021.1.502429796\pythonFiles\lib\python\debugpy\launcher' '58986' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\测试多线程\testThreading4_4.py'
[[1.44, 4, 9], [9, 16, 25], [16, 16, 16], [25, 25, 25]]
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多线程> c:; cd 'c:\Users\chenxuqi\Desktop\News4cxq\测试多线程'; & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2021.1.502429796\pythonFiles\lib\python\debugpy\launcher' '58990' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\测试多线程\testThreading4_4.py'
[[1.44, 4, 9], [9, 16, 25], [16, 16, 16], [25, 25, 25]]
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多线程> c:; cd 'c:\Users\chenxuqi\Desktop\News4cxq\测试多线程'; & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2021.1.502429796\pythonFiles\lib\python\debugpy\launcher' '58996' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\测试多线程\testThreading4_4.py'
[[1.44, 4, 9], [9, 16, 25], [16, 16, 16], [25, 25, 25]]
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多线程>
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多线程>
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多线程>