标签:测试
在阅读代码的时候,碰到了python中多线程的问题。在这里做一个记录
mport time import threading def movie(func): for i in range(2): print "I am watching movie <%s>, time:%s"%(func,time.ctime()) time.sleep(5) def music(func): for i in range(2): print "I am listennig music <%s>, time:%s "%(func,time.ctime()) time.sleep(3) threads=[] t1=threading.Thread(target=movie,args=("Movie funny",)) t2=threading.Thread(target=music,args=("Music funny",)) threads.append(t1) threads.append(t2) if __name__ == ‘__main__‘: for i in threads: i.start() for i in threads: i.join() print "I am done, enjoy it."
输出:
I am watching movie <Movie funny>, time:Mon Jan 04 11:19:38 2016 I am listennig music <Music funny>, time:Mon Jan 04 11:19:38 2016 I am listennig music <Music funny>, time:Mon Jan 04 11:19:41 2016 I am watching movie <Movie funny>, time:Mon Jan 04 11:19:43 2016 I am done, enjoy it. [Finished in 10.2s]
注意时间!以上代码属于函数式多线程。movie和music是同时运行的。然后一个经过了5秒,一个经过了3秒后结束。说明5秒的函数在运行的时候,3秒的函数也在运行。体现了我们写的代码没有错误。这是一个最简单的多线程案例。
使用对象包装多线程:
#!/usr/bin/env python #-*-coding:utf-8-*- import time import threading class ThreadTest(threading.Thread): def __init__(self,name,pause): threading.Thread.__init__(self) self.name=name self.pause=pause self.thread_stop=False def run(self): while not self.thread_stop: print "I am doing %s time : %s"%(self.name,time.ctime()) time.sleep(self.pause) def stop(self): self.thread_stop=True def test(): t1=ThreadTest("movie",5) t2=ThreadTest("music",2) t1.start() t2.start() time.sleep(10) t1.stop() t2.stop() if __name__ == ‘__main__‘: test() print "main done."
这个是使用类来封装多线程。类继承threading。包含初始化,运行,停止三个方法。如果没有停止的话就一直运行多线程里面定义的方法。我们可以看一下输出。证明一下我们的多线程是有效的
I am doing movie time : Mon Jan 04 13:55:33 2016 I am doing music time : Mon Jan 04 13:55:33 2016 I am doing music time : Mon Jan 04 13:55:35 2016 I am doing music time : Mon Jan 04 13:55:37 2016 I am doing movie time : Mon Jan 04 13:55:38 2016 I am doing music time : Mon Jan 04 13:55:39 2016 I am doing music time : Mon Jan 04 13:55:41 2016 I am doing movie time : Mon Jan 04 13:55:43 2016 main done.
一共运行了十秒,才停止。一个运行需要5秒,一个运行需要2秒。是多线程在运行的。
多线程还涉及到了锁,同步等
import random import threading #producer class producer(threading.Thread): def __init__(self,t_name): threading.Thread.__init__(self,name=t_name) def run(self): global x con.acquire() if x>0: con.wait() else: for i in range(5): x+=1 print "producing..."+str(x) con.notify() print x con.release() class consumer(threading.Thread): def __init__(self,t_name): threading.Thread.__init__(self,name=t_name) def run(self): global x con.acquire() if x==0: print "consumer waitting" con.wait() else: for i in range(5): x-=1 print str(x)+" ----\t consuming....%s"%(time.ctime(),) con.notify() print x con.release() con=threading.Condition() x=0 print "start consumer.." c=consumer("consumer") print "start producing.." p=producer("producer") p.start() c.start() p.join() c.join() print "main done"
输出:
start consumer.. start producing.. producing...1 producing...2 producing...3 producing...4 producing...5 5 4 ---- consuming....Mon Jan 04 15:12:39 2016 3 ---- consuming....Mon Jan 04 15:12:39 2016 2 ---- consuming....Mon Jan 04 15:12:39 2016 1 ---- consuming....Mon Jan 04 15:12:39 2016 0 ---- consuming....Mon Jan 04 15:12:39 2016 0 main done [Finished in 0.3s]
这个同步的线程还没有实际应用到。理解不是很深。
本文出自 “ehealth” 博客,谢绝转载!
标签:测试
原文地址:http://ehealth.blog.51cto.com/10942284/1731362