首页 > 编程语言 > 详细

python的学习之旅---信号量 定时器

时间:2017-11-25 22:10:50      阅读:246      评论:0      收藏:0      [点我收藏+]

把线程都创建好,等待执行。

current_thread().getName()

获取当前线程的线程名

from threading import Thread,Semaphore,current_thread
import time,random

sm=Semaphore(5)
def task():
    with sm:
        print(%s 正在上厕所 %current_thread().getName())
        time.sleep(random.randint(1,3))

if __name__ == __main__:
    for i in range(20):
        t=Thread(target=task)
        t.start()

 

定时器

 from threading import   Timer

1 def hello(id):
2     print("hello, world",id)
3 
4 
5 t = Timer(1, hello,args=(30,))
6 t.start()  # after 1 seconds, "hello, world" will be printed

 

python的学习之旅---信号量 定时器

原文:http://www.cnblogs.com/surehunter/p/7896591.html

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