python 信号量_python多线程的信号量

python threading模块还提供古老的信号量(semaphore)功能。

This is one of the oldest synchronization primitives in the history of computer science, invented by the early Dutch computer scientist Edsger W. Dijkstra (he used the names P() and V() instead of acquire() and release()). A semaphore manages an internal counter which is decremented by each acquire() call and incremented by each release() call. The counter can never go below zero; when acquire() finds that it is zero, it blocks, waiting until some other thread calls release().

信号量通过其内部的一个int变量来控制行为,P和V操作时原子的。默认情况下,threading.Semaphore创建的信号量,内部int的初始值为1,此时行为模式与一个lock互斥量没啥区别。不过,有一个细节,信号量的release(即V操作)会增加其内部的int值,而且可以多次release。

>>> import threading

>>> sp = threading.Semaphore()

>>> sp.acquire()

True

>>> sp.release()

>>> sp.release()

信号量多

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值