Python多线程之event

写在前面的话

看了网上一些大牛相关博客,不是很理解,内核对象啊什么的。这篇博客用白话记录自己的学习过程以及一些理解。

event精粹

概述

事件(event)用于线程间同步和通信。比如线程A要完成某一任务(event)线程B才能执行后面的代码,怎么实现呢,就是用event。

event常用方法释义
set()开始一个事件
wait()如果未设置set状态会一直等待,否则过
clear()清除set状态
isSet()是否设置set状态

注意:
wait方法是阻塞的
设置完set后要使用clear方法,否则wait为真,不会等待

上代码

#!/usr/bin python
#coding=utf-8
import threading
import time

def producer():
    print u'等人来买包子....'
    event.wait()
    #event.clear()
    print event.isSet()
    print u'chef:sb is coming for baozi...'
    print u'chef:making a baozi for sb...'
    time.sleep(5)

    print u'chef:你的包子好了...'
    event.set() 

def consumer():
    print u'chenchao:去买包子....'
    event.set()

    time.sleep(2)
    print 'chenchao:waiting for baozi to be ready...'
    print event.wait()
    print u'chenchao:哎呀真好吃....'

event = threading.Event()

p = threading.Thread(target=producer,args=())
c = threading.Thread(target=consumer,args=())
p.start()
c.start()

clear()注释掉的结果

等人来买包子....
chenchao:去买包子....
True
chef:sb is coming for baozi...
chef:making a baozi for sb...
chenchao:waiting for baozi to be ready...
True
chenchao:哎呀真好吃....
chef:你的包子好了...

最后两行显然有问题,包子还没好就可以print了,要使用clear
方法清除设置的set状态。

#!/usr/bin python
#coding=utf-8
import threading
import time

def producer():
    print u'等人来买包子....'
    event.wait()
    event.clear()
    print event.isSet()
    print u'chef:sb is coming for baozi...'
    print u'chef:making a baozi for sb...'
    time.sleep(5)

    print u'chef:你的包子好了...'
    event.set() 

def consumer():
    print u'chenchao:去买包子....'
    event.set()

    time.sleep(2)
    print 'chenchao:waiting for baozi to be ready...'
    print event.wait()
    print u'chenchao:哎呀真好吃....'

event = threading.Event()

p = threading.Thread(target=producer,args=())
c = threading.Thread(target=consumer,args=())
p.start()
c.start()

结果是这样的

等人来买包子....
chenchao:去买包子....
False
chef:sb is coming for baozi...
chef:making a baozi for sb...
chenchao:waiting for baozi to be ready...
chef:你的包子好了...
True
chenchao:哎呀真好吃....

模拟异步模型中的select

#!/usr/bin python
#coding=utf-8
import threading
import time

def producer():
    print u'等人来买包子....'
    event.wait()
    event.clear()
    print event.isSet()
    print u'chef:sb is coming for baozi...'
    print u'chef:making a baozi for sb...'
    time.sleep(5)

    print u'chef:你的包子好了...'
    event.set() 

def consumer():
    print u'chenchao:去买包子....'
    event.set()

    time.sleep(2)
    print 'chenchao:waiting for baozi to be ready...'
    while True:
        if event.isSet():
            print u'真好吃...'
            break
        else:
            print u'好慢啊...'
            time.sleep(1)#相当于去做自己的事去了
event = threading.Event()
p = threading.Thread(target=producer,args=())
c = threading.Thread(target=consumer,args=())
p.start()
c.start()

结果

等人来买包子....
chenchao:去买包子....
False
chef:sb is coming for baozi...
chef:making a baozi for sb...
chenchao:waiting for baozi to be ready...
好慢啊...
好慢啊...
好慢啊...
好慢啊...chef:你的包子好了...

真好吃...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值