• python 多线程编程


    这篇文章我是根据【莫烦Python】Threading 学会多线程 Python的视频做的学习记录。
    莫烦Python好帅好可爱哈哈哈。

    函数名功能
    threading.active_count()目前有多少个激活的线程
    threading.enumerate()激活的线程是哪几个
    threading.current_thread()正在运行的是哪个线程

    添加线程

    import threading
    
    
    def thread_job():
        print('This is an added Thread, number id %s' % threading.current_thread())
    
    
    def main():
        added_thread = threading.Thread(target=thread_job)  # 创建线程
        added_thread.start()  # 启动线程
    
    
    if __name__ == '__main__':
        main()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    join功能——合并

    首先思考几个问题:

    1. 怎样实现一个线程和主线程合并?
    2. 如果有多个线程,我想等所有线程结束再接着运行,应该怎么实现?
    import threading
    import time
    
    
    def thread_job():
        print('T1 start')
        for i in range(10):
            time.sleep(0.1)
        print('T1 finish')
    
    
    def t2_job():
        print('T2 start')
        print('T2 finish')
    
    
    def main():
        added_thread = threading.Thread(target=thread_job, name='T1')
        thread2 = threading.Thread(target=t2_job)
        added_thread.start()
        thread2.start()
        added_thread.join()
        thread2.join()
        print('all down!')
    
    
    if __name__ == '__main__':
        main()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    Queue功能——值传递

    可以往线程里面传递参数,但是线程不能有返回值。
    以下实例的目的是将data列表中的值全部平方:

    import threading
    import time
    from queue import Queue
    
    # 需要修改的列表数据
    data = [[1, 2, 3], [3, 4, 5], [4, 4, 4], [5, 5, 5]]
    
    
    # 定义一个功能函数,将列表中的值平方
    def job(l):
        for i in range(len(l)):
            l[i] = l[i]**2
        return l
    
    
    # 如果不用多线程,用以下方式实现
    def no_threading():
        result = []
        for d in data:
            r = job(d)
            result.append(r)
        print(result)
    
    
    def job2(l, q):
        for i in range(len(l)):
            l[i] = l[i]**2
        q.put(l)
    
    
    def multithreading():
    
        q = Queue()
        threads = []
        # 启动线程
        for i in range(4):
            t = threading.Thread(target=job2, args=(data[i], q))
            t.start()
            threads.append(t)
        # 回收线程
        for thread in threads:
            thread.join()
    
        # 获取数据
        results = []
        for _ in range(4):
            results.append(q.get())
        print(results)
    
    
    if __name__ == '__main__':
        # no_threading()
        multithreading()
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    线程锁

    import threading
    
    
    def job1():
        global A, lock
        lock.acquire()
        for i in range(10):
            A += 1
            print('job1', A)
        lock.release()
    
    
    def job2():
        global A, lock
        lock.acquire()
        for i in range(10):
            A += 10
            print('job2', A)
        lock.release()
    
    
    if __name__ == '__main__':
        A = 0
        lock = threading.Lock()
        t1 = threading.Thread(target=job1)
        t2 = threading.Thread(target=job2)
        t1.start()
        t2.start()
        t1.join()
        t2.join()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
  • 相关阅读:
    ArmSoM-RK3588编解码之mpp解码demo解析:mpi_dec_test
    Linux 内存之vmstat
    机器学习笔记 - 常用概率分布
    Vmware设置共享文件夹实现与ubuntu文件共享
    (一)JavaScript 面向对象
    我的创作纪念日——2048天
    订单正逆向流程
    快手根据ID取商品详情 API 返回值说明
    后台管理----新建和编辑hooks 封装
    ArrayList 可以完全替代数组吗?
  • 原文地址:https://blog.csdn.net/weixin_42442319/article/details/125533393