标签:
1 #!/usr/bin/env python 2 3 from time import sleep, ctime 4 5 def loop0(): 6 print ‘start loop 0 at:‘, ctime() 7 sleep(4) 8 print ‘loop 0 done at:‘, ctime() 9 10 def loop1(): 11 print ‘start loop 1 at:‘, ctime() 12 sleep(2) 13 print ‘loop 1 done at:‘, ctime() 14 15 def main(): 16 print ‘starting at:‘, ctime() 17 loop0() 18 loop1() 19 print ‘all DONE at:‘, ctime() 20 21 if __name__ == ‘__main__‘: 22 main()
1 #!/usr/bin/env python 2 3 import thread 4 from time import sleep, ctime 5 6 def loop0(): 7 print ‘start loop 0 at:‘, ctime() 8 sleep(4) 9 print ‘loop 0 done at:‘, ctime() 10 11 def loop1(): 12 print ‘start loop 1 at:‘, ctime() 13 sleep(2) 14 print ‘loop 1 done at:‘, ctime() 15 16 def main(): 17 print ‘starting at:‘, ctime() 18 thread.start_new_thread(loop0, ()) 19 thread.start_new_thread(loop1, ()) 20 sleep(6) 21 print ‘all DONE at:‘, ctime() 22 23 if __name__ == ‘__main__‘: 24 main()
使用线程和锁 (mtsleep2.py)
1 #!/usr/bin/env python 2 3 import thread 4 from time import sleep, ctime 5 6 loops = [4,2] 7 8 def loop(nloop, nsec, lock): 9 print ‘start loop‘, nloop, ‘at:‘, ctime() 10 sleep(nsec) 11 print ‘loop‘, nloop, ‘done at:‘, ctime() 12 lock.release() 13 14 def main(): 15 print ‘starting at:‘, ctime() 16 locks = [] 17 nloops = range(len(loops)) 18 19 for i in nloops: 20 lock = thread.allocate_lock() 21 lock.acquire() 22 locks.append(lock) 23 24 for i in nloops: 25 thread.start_new_thread(loop, (i, loops[i], locks[i])) 26 27 for i in nloops: 28 while locks[i].locked(): pass 29 30 print ‘all DONE at:‘, ctime() 31 32 if __name__ == ‘__main__‘: 33 main()
18.5 threading 模块
1 #!/usr/bin/env python 2 3 import threading 4 from time import sleep, ctime 5 6 loops = [4,2] 7 8 def loop(nloop, nsec): 9 print ‘start loop‘, nloop, ‘at:‘, ctime() 10 sleep(nsec) 11 print ‘loop‘, nloop, ‘done at:‘, ctime() 12 13 def main(): 14 print ‘starting at:‘, ctime() 15 threads = [] 16 nloops = range(len(loops)) 17 18 for i in nloops: 19 t = threading.Thread(target=loop,20 args=(i, loops[i])) 21 threads.append(t) 22 23 for i in nloops: # start threads 24 threads[i].start() 25 26 for i in nloops: 27 threads[i].join() # threads to finish 28 29 print ‘all DONE at:‘, ctime() 30 31 if __name__ == ‘__main__‘: 32 main()
1 #!user/env/bin python 2 3 import threading 4 from time import sleep, ctime 5 6 loops = [4,2] 7 8 class Thread4(threading.Thread): 9 def __init__(self, nloop, nsec, 10 group=None, target=None, name=None,11 args=(), kwargs=None, verbose=None): 12 self.nloop = nloop 13 self.nsec = nsec 14 threading.Thread.__init__(self) 15 def run(self): 16 print ‘start loop‘, self.nloop, ‘at:‘, ctime() 17 sleep(self.nsec) 18 print ‘loop‘, self.nloop, ‘done at:‘, ctime() 19 20 def main(): 21 print ‘starting at:‘, ctime() 22 threads = list() 23 nloops = range(len(loops)) 24 25 for i in nloops: 26 t = Thread4(i, loops[i]) 27 threads.append(t) 28 29 for i in nloops: 30 threads[i].start() 31 32 for i in nloops: 33 threads[i].join() 34 35 print ‘all DONE at:‘, ctime() 36 37 if __name__ == ‘__main__‘: 38 main()
1 #!usr/bin/env python 2 3 from myThread import MyThread 4 from time import time 5 6 def fabonacci(n): 7 if n <= 2: 8 return 1 9 if n > 2: 10 return fabonacci(n-1) + fabonacci(n-2) 11 12 def factorial(n): 13 if n <= 1: 14 return 1 15 if n > 1: 16 return factorial(n-1) * n 17 18 def count(n): 19 sum = 0 20 for i in range(n+1): 21 sum += i 22 return sum 23 24 def main(): 25 t1 = time() 26 threads = list() 27 loops = [32,10,100000] 28 29 threads.append(MyThread(fabonacci, (loops[0],))) 30 threads.append(MyThread(factorial, (loops[1],))) 31 threads.append(MyThread(count, (loops[2],))) 32 33 for t in threads: 34 t.start() 35 36 for t in threads: 37 t.join() 38 39 for t in threads: 40 print t.func.__name__, ‘(%d)=‘ % t.args[0],41 t.getResult(), ‘time:%f‘ % t.getts() 42 t2 = time() 43 print ‘All time:%f‘ % (t2-t1) 44 45 t1 = time() 46 fabonacci(loops[0]) 47 factorial(loops[1]) 48 count(loops[2]) 49 t2 = time() 50 print ‘%f‘ % (t2-t1) 51 52 53 if __name__ == ‘__main__‘: 54 main() 55
1 #!usr/bin/env python 2 3 import threading 4 from time import time 5 6 class MyThread(threading.Thread): 7 def __init__(self, func, args): 8 self.func = func 9 self.args = args 10 self.res = None 11 self.ts = 0 12 threading.Thread.__init__(self) 13 14 def run(self): 15 t1 = time() 16 self.res = apply(self.func, self.args) 17 t2 = time() 18 self.ts = t2 - t1 19 20 def getResult(self): 21 return self.res 22 23 def getts(self): 24 return self.ts
threading 模块中的其它函数
1 #!/usr/bin/env python 2 3 from random import randint 4 from time import sleep 5 from Queue import Queue 6 from myThread import MyThread 7 8 def writeQ(queue): 9 print ‘producting object for Q...‘ 10 queue.put(‘product‘, 1) 11 print "size now %d" % queue.qsize() 12 13 def readQ(queue): 14 val = queue.get(1) 15 print ‘consumed object from Q... size now, %d‘ %16 queue.qsize() 17 18 def writer(queue, loops): 19 for i in range(loops): 20 writeQ(queue) 21 sleep(randint(1, 3)) 22 23 def reader(queue, loops): 24 for i in range(loops): 25 readQ(queue) 26 sleep(randint(2, 5)) 27 28 funcs = [writer, reader] 29 nfuncs = range(len(funcs)) 30 31 def main(): 32 nloops = randint(2, 5) 33 q = Queue(32) 34 35 threads = [] 36 for i in nfuncs: 37 t = MyThread(funcs[i], (q, nloops)) 38 threads.append(t) 39 40 for i in nfuncs: 41 threads[i].start() 42 43 for i in nfuncs: 44 threads[i].join() 45 46 print ‘all DONE‘ 47 48 if __name__ == ‘__main__‘: 49 main()
18.6 相关模块
标签:
原文地址:http://www.cnblogs.com/BugQiang/p/4743106.html