python中condition条件变量的作用
1、Python提供的Condition对象支持复杂的线程同步。
2、Condition被称为条件变量,除了提供类似Lock的acquire和release方法外,还提供wait和notify方法。线程先acquire条件变量,然后判断一些条件。
实例
import threading, time class Hider(threading.Thread): def __init__(self, cond, name): super(Hider, self).__init__() self.cond = cond self.name = name def run(self): time.sleep(1) #确保先运行Seeker中的方法 self.cond.acquire() #b print(self.name + ': 我已经把眼睛蒙上了') self.cond.notify() self.cond.wait() #c #f print(self.name + ': 我找到你了 ~_~') # self.cond.notify() self.cond.release() #g print(self.name + ': 我赢了') #h class Seeker(threading.Thread): def __init__(self, cond, name): super(Seeker, self).__init__() self.cond = cond self.name = name def run(self): self.cond.acquire() self.cond.wait() #a #释放对琐的占用,同时线程挂起在这里,直到被notify并重新占有琐。 #d print(self.name + ': 我已经藏好了,你快来找我吧') self.cond.notify() self.cond.wait() #e #h self.cond.release() print(self.name + ': 被你找到了,哎~~~') cond = threading.Condition() seeker = Seeker(cond, 'seeker') hider = Hider(cond, 'hider') seeker.start() hider.start()
微信扫描下方的二维码阅读更多精彩内容
每日分享到群里,或者推荐给朋友会得大量积分,机会可以兑换微信零钱红包,具体请点击这里,得到了微信红包可以用来支持大飞哥
大飞哥能不能加鸡腿就看各位了!
开发者微信
开发者微信反馈BUG或者VIP可以添加,其他情况反馈可能不及时,见谅
版权声明
初衷是提供高清手机电脑壁纸等图片素材免费分享下载,禁止商用。图片素材来源网络,版权归原作者所有,若有侵权问题敬请告知我们!
【友情提醒】:
因平台原因不易展示大尺度写真,有的写真展示越少代表此套写真越性感,特别是xiuren等写真每一套写真完整套图50-100张不等。更多内容的欣赏请移步 点击这里
【更多图集移步】:
每日更新-点击这里
漂亮小姐姐-点击这里
性感美女-点击这里
清纯女孩-点击这里
xiuren专栏-点击这里
整站资源下载-点击这里