python多线程模块threading学习笔记(3)之Queue的使用

参考链接: 【莫烦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\测试多线程> 

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值