首页 > 编程语言 > 详细

Python信号量

时间:2018-03-09 16:30:39      阅读:173      评论:0      收藏:0      [点我收藏+]

 

信号量相当于一个锁锁着的房间里的多个床位,同一时间可以有多个线程获得信号量,而锁则只能被一个线程获得。

import threading
import time

semaphore = threading.Semaphore(3)

def func():
    if semaphore.acquire():   # acquire和release方法同锁的方法,不同在于可以控制多个线程得到该semaphore
        print (threading.currentThread().getName() +  get semaphore)
        time.sleep(1)
        semaphore.release()

for i in range(10):
  t1 = threading.Thread(target=func)
  t1.start()

 

Python信号量

原文:https://www.cnblogs.com/stin/p/8534604.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!