• 技术文章 >Python技术 >Python基础教程

    python线程中Condition的原理

    小妮浅浅小妮浅浅2021-08-23 09:29:54原创3599

    原理分析

    1、Python条件变量Condition需要关联互斥锁,同时Condition本身提供了wait、notify、notifyAll方法。

    2、用于阻塞、通知其他并行线程,可以访问共享资源。

    Condition提供了一种多线程通信机制。如果线程1需要数据,线程1会堵塞等待,然后线程2会制造数据。线程2制造数据后,通知线程1可以获取数据,然后线程1会获取数据。

    实例

    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

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    71

    72

    73

    74

    75

    76

    77

    78

    79

    80

    81

    82

    83

    84

    85

    86

    87

    88

    89

    90

    91

    # !usr/bin/env python

    # -*- coding:utf-8 _*-

    # 导入线程模块

    import threading

      

    # 创建条件变量condition

    con = threading.Condition()

      

    def thread_one(name):

        # 条件变量condition 线程上锁

        con.acquire()

      

        print("{}:成语接龙准备好了吗".format(name))

        # 唤醒正在等待(wait)的线程

        con.notify()

      

        # 等待对方回应消息,使用wait阻塞线程,等待对方通过notify唤醒本线程

        con.wait()

        print("{}:一干二净".format(name))

        # 唤醒对方

        con.notify()

      

        # 等待消息答应

        con.wait()

        print("{}:一天就知道看抖音美女,给你来个简单点的,来了:毛手毛脚".format(name))

        # 唤醒对方

        con.notify()

      

        # 等待消息答应

        con.wait()

        print("{}:哟哟哟,不错不错!".format(name))

        # 唤醒对方

        con.notify()

      

        # 条件变量condition 线程释放锁

        con.release()

      

    def thread_two(name):

        # 条件变量condition 线程上锁

        con.acquire()

      

        # wait阻塞状态,等待其他线程通过notify唤醒本线程

        con.wait()

        print("{}:准备好了~开始吧!".format(name))

        # 唤醒对方

        con.notify()

      

        # 等待消息答应

        con.wait()

        print("{}:净你妹啊,没法接...来个简单点的...".format(name))

        # 唤醒对方

        con.notify()

      

        # 等待消息答应

        con.wait()

        print("{}:嘿,这个我知道:脚踏实地".format(name))

        # 唤醒对方

        con.notify()

      

        con.release()

      

    if __name__ == "__main__":

      

        # 创建并初始化线程

        t1 = threading.Thread(target=thread_one,args=("A"))

        t2 = threading.Thread(target=thread_two,args=("B"))

      

        # 启动线程 -- 注意线程启动顺序,启动顺序很重要

        t2.start()

        t1.start()

      

        # 阻塞主线程,等待子线程结束

        t1.join()

        t2.join()

      

      

        print("程序结束!")

      

      

    '''

    输出结果:

      

    A:成语接龙准备好了吗

    B:准备好了~开始吧!

    A:一干二净

    B:净你妹啊,没法接...来个简单点的...

    A:一天就知道看抖音美女,给你来个简单点的,来了:毛手毛脚

    B:嘿,这个我知道:脚踏实地

    A:哟哟哟,不错不错!

    程序结束!

    '''

    以上就是python线程中Condition的原理,希望对大家有所帮助。更多Python学习指路:python基础教程

    本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

    专题推荐:python线程 condition
    上一篇:python中del在垃圾回收的使用 下一篇:python线程事件Event的原理

    相关文章推荐

    • 进一步认识python线程池• python线程用什么模块好?• Python线程中的阻塞是什么?• python线程中的GIL如何使用?• 如何使用python线程start和run方法?• python线程强制停止工作• python线程中deque如何使用?• python线程优先级队列有哪些?• python线程阻塞的解决• python线程安全的介绍及解决方法

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网