python多线程threading事件对象event实现线程阻塞及timer时间对象
来源:互联网 发布:js获取div自定义属性 编辑:程序博客网 时间:2024/06/11 00:02
事件对象:
可以用于简单的进程之间的通信,当线程需执行其他操作时,阻塞线程
class threading.Event
1.is_set() / isSet()
2.set()
3.clear()
4.wait([timeout])
示例:
# encoding: UTF-8import threadingimport timeevent = threading.Event()def FunEvent(): print '%s -- %s start wait ...' % (time.ctime(),threading.currentThread().getName()) event.wait() print '%s -- %s end wait ...' % (time.ctime(), threading.currentThread().getName())def bet_fun(): print '%s enter bet_fun '%time.ctime() time.sleep(1) print '%s out bet_fun ' % time.ctime()Th1 = threading.Thread(target=FunEvent,name="Th-1")Th2 = threading.Thread(target=FunEvent,name="Th-2")Th1.start()Th2.start()time.sleep(1)bet_fun()event.set()
结果:
Sun May 28 23:25:12 2017 -- Th-1 start wait ...Sun May 28 23:25:12 2017 -- Th-2 start wait ...Sun May 28 23:25:13 2017 enter bet_fun Sun May 28 23:25:14 2017 out bet_fun Sun May 28 23:25:14 2017 -- Th-1 end wait ... Sun May 28 23:25:14 2017 -- Th-2 end wait ...
时间对象:
class threading.Timer(interval, function, args=[], kwargs={})
interval时间,单位秒
function函数名称
方法:
1.start()
2.cancel()
例如:
def hello(): print "hello, world"t = threading.Timer(3, hello)t.start()
阅读全文
0 0
- python多线程threading事件对象event实现线程阻塞及timer时间对象
- python中threading的高级函数应用解析 Lock Rlock Condition Semaphore Timer Event Semaphore对象
- python threading/Event & Timer(3)
- Python:使用threading模块实现多线程编程八[使用Event实现线程间通信]
- Python:使用threading模块实现多线程编程八[使用Event实现线程间通信]
- 关于Python的进程线程协程之threading模块(三)Event对象
- Python多线程之threading Event
- Python线程之threading.Event
- 多线程同步之Event(事件对象)
- 线程同步 事件 Event 内核对象 CreateEvent
- 【Python】threading.Event模块控制多线程
- 多线程编程Demo[利用事件对象实现线程同步]
- 多线程实现线程同步——事件对象
- windows多线程系列003 利用事件对象实现线程同步
- 多线程编程(13) - 多线程同步之 Event (事件对象)
- Event事件对象之Event
- [Python]多线程--threading模块实现
- python模块threading实现多线程
- python使用threading.condition实现生产者消费者
- javascript不同的循环方式
- python多线程threading使用Semaphore或BoundedSemaphore实现并发限制
- 393. UTF-8 Validation
- 颜色类HSSFColor
- python多线程threading事件对象event实现线程阻塞及timer时间对象
- 阿里云服务器安装完apache通过IP访问无反应
- js中的map()方法
- IPC实现机制(一)---pipe(匿名管道)
- java自定义异常
- Hadoop-2.8.0集群搭建、hadoop源码编译和安装、host配置、ssh免密登录、hadoop配置文件中的参数配置参数总结、hadoop集群测试,安装过程中的常见错误
- leetcode.412. Fizz Buzz
- Spring Boot有四大神器之actuator
- xuotoj 1510: 输入数值区间,求素数