Python Thread related
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python Thread related,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3941字,纯文字阅读大概需要6分钟。
内容图文
1.Thread.join([timeout])
Wait until the thread terminates. This blocks the calling thread until the thread whose join() method is called terminates – either normally or through an unhandled exception – or until the optional timeout occurs.
等待进程结束。也就是说,其屏蔽调用线程,直到此线程方法终止(要么正常执行完毕,或者未处理的异常,或者时间超时)
下面通过例子来说明:
没有设定timeout情况,Main 线程启动,work1 和work2 线程执行,完毕退出,Main线程执行终止
1 import os 2 import threading 3 import time 4 import logging 5 import random 6 7 def work1(): 8 count=0 9while count<=5: 10 threadname= threading.currentThread() 11 wait_time=random.randrange(1,4) 12print("%s,count =%s wait for =%s s,time %s "%(threadname,count,wait_time,time.ctime()[-13:])) 13 time.sleep(wait_time) 14 count +=1 1516def work2(): 17 i=0 18while i<=5: 19 threadname= threading.currentThread() 20 wait_time=random.randrange(1,4) 21print("%s,i =%s wait for =%s s,time %s "%(threadname,i,wait_time,time.ctime()[-13:])) 22 time.sleep(wait_time) 23 i +=1 2425if__name__ =="__main__": 26 mainthread= threading.currentThread() 27print‘%s main thread is waiting for exit‘% mainthread 28 test1=threading.Thread(name=‘work1‘,target=work1) 29 test2=threading.Thread(name=‘work2‘,target=work2) 30 test1.start() 31 test2.start() 32 test1.join() 33 test2.join() 34print‘main thread finish‘
2个线程设定超时时间work1 5s,work2 4s,9s之后调用线程结束而不等待超时的线程:
1 import os 2 import threading 3 import time 4 import logging 5 import random 6 7 def work1(): 8 count=0 9while count<=5: 10 threadname= threading.currentThread() 11 wait_time=random.randrange(1,4) 12print("%s,count =%s wait for =%s s,time %s "%(threadname,count,wait_time,time.ctime()[-13:])) 13 time.sleep(wait_time) 14 count +=1 1516def work2(): 17 i=0 18while i<=5: 19 threadname= threading.currentThread() 20 wait_time=random.randrange(1,4) 21print("%s,i =%s wait for =%s s,time %s "%(threadname,i,wait_time,time.ctime()[-13:])) 22 time.sleep(wait_time) 23 i +=1 2425if__name__ =="__main__": 26 mainthread= threading.currentThread() 27print‘%s main thread is waiting for exit‘% mainthread 28 test1=threading.Thread(name=‘work1‘,target=work1) 29 test2=threading.Thread(name=‘work2‘,target=work2) 30 test1.start() 31 test2.start() 32 test1.join(4) 33 test2.join(5) 34print‘main thread finish
2.Producer and comsumer
1 import Queue 2 import threading 3 import random 4 5 # writelock =threading.Lock() 6 class Producer(threading.Thread): 7 def __init__ (self,q,con,name): 8 super(Producer,self).__init__() 9 self.q = q 10 self.con = con 11 self.name = name 12print"produce" +self.name+"started"13def run(self): 14while 1: 15#global writelock 16 self.con.acquire()#acquire the lock 17if self.q.full():#if queue is full18#with writelock:#output info19print"queue is full, producer wait"20 self.con.wait()#wait for resource21else: 22 value = random.ranint(0,10) 23#with writelock:24print self.name+"put value"+self.name+":"+str(value)+"into queue"25 self.q.put((self.name+":"+str(value)))#put to queue26 self.con.notify()#inform consumer27 self.con.release()#release the lock2829class Consumer(threading.Thread): 30def__init__(self,q,con,name): 31 super(Consumer,self).__init__() 32 self.q = q 33 self.con = con 34 self.name = name 35print"consume" +self.name+"started\n"36def run(self): 37while 1: 38#global writelock39 self.con.acquire() 40if self.q.empty():#if empty41#with writelock:42print"queue is empty,consumer wait"43 self.con.wait()#wait the resource ready44else: 45 value = self.q.get()#get one element from queue46#with writelock:47print self.name +"get value"+ value+"from queue"48 self.q.notify()#inform producer49 self.con.release()#release the lock505152if__name__ == "__main__": 53print"start to run\n"54 q = Queue.Queue(10) 55 con = threading.Condition() 56 p = Producer(q,con,"p1") 57 p.start() 58 p1 = Producer(q,con,"p2") 59 p1.start() 60 c1 = Consumer(q,con,"c1") 61 c1.start() 626364656667
3.待续......
原文:http://www.cnblogs.com/lxk613/p/4786270.html
内容总结
以上是互联网集市为您收集整理的Python Thread related全部内容,希望文章能够帮你解决Python Thread related所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。