【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 @ 2020-11-21 18:06  淡怀  阅读(82)  评论(0编辑  收藏  举报