【Python】多线程

https://blog.csdn.net/l835311324/article/details/86608850

https://blog.csdn.net/zhangphil/article/details/88577091

https://www.cnblogs.com/cnhyk/p/13697121.html

 

一、不需要获取线程执行结果

复制代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
@Time    :
@Author  :
@File    :
@Version :1.0
@Function:
"""
import threading
import time


class Th:
    @staticmethod
    def task(count):
        if count == 5:
            time.sleep(count)
            print(f'任务{count},休眠{count}秒')
        elif count == 8:
            time.sleep(count - 7)
            print(f'任务{count},休眠{count - 7}秒')
        elif count == 10:
            time.sleep(count - 9)
            print(f'任务{count},休眠{count - 9}秒')
        else:
            print(f'任务{count}')
        return count


if __name__ == '__main__':
    tt_list = []
    # 创建11个线程并运行
    for i in range(11):
        tt = threading.Thread(target=Th.task, args=(i,))
        tt.start()
        tt_list.append(tt)
    # join表示等待所有线程结束
    for i in tt_list:
        i.join()
    # 所有线程结束后 才会运行下面的
    print(*tt_list, sep='\n')
复制代码

输出:

任务0
任务1
任务2
任务3
任务4
任务6
任务7
任务9
任务10,休眠1秒
任务8,休眠1秒
任务5,休眠5秒
<Thread(Thread-1, stopped 15196)>
<Thread(Thread-2, stopped 14260)>
<Thread(Thread-3, stopped 6256)>
<Thread(Thread-4, stopped 14420)>
<Thread(Thread-5, stopped 15284)>
<Thread(Thread-6, stopped 14664)>
<Thread(Thread-7, stopped 13724)>
<Thread(Thread-8, stopped 1952)>
<Thread(Thread-9, stopped 1064)>
<Thread(Thread-10, stopped 13376)>
<Thread(Thread-11, stopped 13292)>

 

二、需要获取线程执行结果

把线程执行结果放到全局变量里即可

复制代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
@Time    :
@Author  :
@File    :
@Version :1.0
@Function:
"""
import threading
import time


class Th:
    thread_result = []

    @staticmethod
    def task(count):
        if count == 5:
            time.sleep(count)
            print(f'任务{count},休眠{count}秒')
        elif count == 8:
            time.sleep(count - 7)
            print(f'任务{count},休眠{count - 7}秒')
        elif count == 10:
            time.sleep(count - 9)
            print(f'任务{count},休眠{count - 9}秒')
        else:
            print(f'任务{count}')
        # 把执行结果放到全局变量里
        Th.thread_result.append({
            'input': count,
            'output': count
        })
        return count


if __name__ == '__main__':
    tt_list = []
    # 创建11个线程并运行
    for i in range(11):
        tt = threading.Thread(target=Th.task, args=(i,))
        tt.start()
        tt_list.append(tt)
    # join表示等待所有线程结束
    for i in tt_list:
        i.join()
    # 所有线程结束后 才会运行下面的
    print(*tt_list, sep='\n')
    print(*Th.thread_result, sep='\n')
复制代码

输出:

任务0
任务1
任务2
任务3
任务4
任务6
任务7
任务9
任务10,休眠1秒
任务8,休眠1秒
任务5,休眠5秒
<Thread(Thread-1, stopped 13108)>
<Thread(Thread-2, stopped 10684)>
<Thread(Thread-3, stopped 3556)>
<Thread(Thread-4, stopped 14076)>
<Thread(Thread-5, stopped 11992)>
<Thread(Thread-6, stopped 13620)>
<Thread(Thread-7, stopped 15184)>
<Thread(Thread-8, stopped 14356)>
<Thread(Thread-9, stopped 9328)>
<Thread(Thread-10, stopped 14376)>
<Thread(Thread-11, stopped 13084)>
{'input': 0, 'output': 0}
{'input': 1, 'output': 1}
{'input': 2, 'output': 2}
{'input': 3, 'output': 3}
{'input': 4, 'output': 4}
{'input': 6, 'output': 6}
{'input': 7, 'output': 7}
{'input': 9, 'output': 9}
{'input': 10, 'output': 10}
{'input': 8, 'output': 8}
{'input': 5, 'output': 5}

 

posted @   淡怀  阅读(82)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义

目录导航

点击右上角即可分享
微信分享提示