码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-11-25 22:37:01      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:reading   read   task   rand   target   int   map   执行   and   

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

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的学习之旅---信号量 定时器

标签:reading   read   task   rand   target   int   map   执行   and   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!