• 多线程threading


    1、Python自带多线程模块 _thread
    _thread.start_new_thread(function, (参数元组)), function结束,线程也结束

    2、第三方模块 threading, 要重写 init 和 run()

    import threading
    class A(threading.Thread):
        def __init__(self, ):
            thread.Thread.__init__(self)    # 一定要有的
    thread1 = A(参数)
    thread1.start()    # 启动线程
    thread1.exit()    # 结束线程
    thread1.isAlive()    # 线程是否活动
    threading.currentThread()    # 返回当前线程变量
    threading.enumerate()    # 返回正在运行的线程列表
    threading.activeCount()    # 或者len(threading.enumerate()) 返回正在运行的线程数
    
    

    3、.join() 代表即使主程序结束,也要执行完开始的线程才退出

    4、强行要求一个线程结束后再执行另一线程,使用线程同步技术

    • 锁同步(锁定和解锁必须成对出现,不然会死锁)
      将一线程中的run()方法用锁定和解锁包围即可
    threading.Lock()    #线程锁对象,在文件开头创建,后面的类中可直接使用
    threading.Lock().acqure()    # 锁定
    threading.Lock().release()    # 解锁
    
    • 条件变量同步
      条件变量 Condition 对象,除了acqure,release 还有 wait, notify, notifyAll(用来唤醒其他阻塞线程)方法
    import threading
    from threading import Condition
     
     
    class A(threading.Thread):
        def __init__(self, condition, num):
            self.condition = condition
            self.num = num
            super().__init__(name="A")
     
        def run(self):
            with self.condition:
                for i in range(self.num):
                    print("A say: %s + %s 等于多少 ?" % (str(i), str(i)))
                    self.condition.notify()
                    self.condition.wait()
     
     
    class B(threading.Thread):
        def __init__(self, condition, num):
            self.condition = condition
            self.num = num
            super().__init__(name="B")
     
        def run(self):
            self.condition.acquire() # 必须先获得锁才可以调用wait或者notify
            # with self.condition:  # 这里没有使用with,其实如果不使用with你就手动加锁,然后释放。使用with就帮你自动完成,这就和 with open()自动关闭文件流式一样的。
            for i in range(self.num):
                self.condition.wait()
                print("B say: %s + %s 等于 %s." % (str(i), str(i), str(i+i)))
                self.condition.notify()
            self.condition.release()
     
    if __name__ == '__main__':
        condition = Condition()
        a = A(condition, 5)
        b = B(condition, 5)
     
        """
        这里要注意,虽然是a先说话,但是要先启动b,使用了condition的话线程的启动顺序很重要。如果先启动a,那么就会一直卡住。为什么?
        先启动a,那么就会先执行notify,而b还没有启动也就意味着没有wait,所以卡住了,也就是说notify的前提是先有wait才可以。换句话
        说就是你要通知就得先有人在等,在没人等的情况下你通知了这样你就无法获得回复所以就一直卡在那里。所以先启动b就是先等待,然后启动a
        a说完话就通知,这样b可以收到然后处理,然后b回复,之后唤醒a,这样相继执行。
        其实Condition和event是类似的可以理解为基于事件的通知机制。
     
        注意事项: 1. 先调用 with self.condition 或者是 acquire() 也就是先加锁才可以使用wait和notify
                 2. 如果手动加锁之后要手动释放
                 3. 线程启动顺序很重要
        """
        b.start()
        a.start()
    

  • 相关阅读:
    对数线性模型与线性链条件随机场
    25匹马,5个跑道,每个跑道最多能有1匹马进行比赛,最少比多少次能比出前3名?前5名?
    SVM 与 LR的异同
    EM算法简易推导
    K-means算法的优缺点
    自助采样包含训练集里63.2%的样本?
    指数加权移动平均
    oracle 对于用户的相关操作
    docker 安装 maven 私有库 nexus3
    idea 自动注入@Autowired 警告 Field injection is not recommended 关闭
  • 原文地址:https://www.cnblogs.com/wolun666/p/14663107.html
Copyright © 2020-2023  润新知