#!/usr/bin/python# -*- coding: utf-8 -*-__author__ = 'gaogd''''### 多进程import threadingimport timedef run(num): print 'Hi, I am thread %s..lalala' % num time.sleep(1)for i in range(20): t = threading.Thread(target=run, args=(i,)) t.start()''''''##进程启动完20个线程就继续走下去,不等待这20个线程走完就回到住线程import threadingimport timedef run(num): global NUM time.sleep(1) print 'Hi, I am thread %s..lalala' % num NUM +=1NUM = 0for i in range(20): t = threading.Thread(target=run, args=(i,)) t.start() ###time.sleep(2)print '------>',NUM''''''import threadingimport timedef run(num): global NUM time.sleep(1) print 'Hi, I am thread %s..lalala' % num NUM +=1NUM = 0for i in range(20): t = threading.Thread(target=run, args=(i,)) t.start() t.join() ##等子线程执行完再执行子线程。print '------>',NUM''''''###下面两百个线程时并发执行,但是共享内存有点问题import threadingimport timedef run(num): global NUM time.sleep(1) print 'Hi, I am thread %s..lalala' % num NUM +=1NUM = 0p_list = [] ##线程列表,追加线程实例 t 到这个列表for i in range(500): t = threading.Thread(target=run, args=(i,)) t.start() p_list.append(t)for i in p_list: t.join() ##等待线程都执行完再执行这个,但是共享的内存有可能出现问题,所有出现NUM不一定等于500,没有加锁导致的问题,这就涉及安全了print '------>',NUM'''import threadingimport timedef run(num): global NUM time.sleep(1) print 'Hi, I am thread %s..lalala' % num lock.acquire() ## 加锁 NUM +=1 lock.release() ##释放锁,如果不释放,那后面的就都改不了数据了NUM = 0p_list = []lock = threading.Lock() ##生成一把锁for i in range(500): t = threading.Thread(target=run, args=(i,)) t.start() p_list.append(t)for i in p_list: t.join() ##等待线程都执行完再执行这个,但是共享的内存有可能出现问题,所有出现NUM不一定等于200,没有加锁导致的问题,这就涉及安全了print '------>',NUM
Event
#_*_coding:utf-8_*___author__ = 'jieli'import threading,timeimport randomdef light(): if not event.isSet(): event.set() #wait就不阻塞 #绿灯状态 count = 0 while True: if count < 10: print '\033[42;1m--green light on---\033[0m' elif count <13: print '\033[43;1m--yellow light on---\033[0m' elif count <20: if event.isSet(): event.clear() #clear the green light , switch on the red light print '\033[41;1m--red light on---\033[0m' else: count = 0 event.set() #打开绿灯 time.sleep(1) count +=1def car(n): while 1: time.sleep(random.randrange(3)) if event.isSet(): #绿灯 print "car [%s] is running.." % n else:# red light print "car [%s] is waiting for the red light.." %n event.wait() print "Green light is on , car %s is running.............." %nif __name__ == '__main__': event = threading.Event() Light = threading.Thread(target=light) Light.start() for i in range(3): t = threading.Thread(target=car,args=(i,)) t.start()'''Events #An event is a simple synchronization object;the event represents an internal flag, and threadscan wait for the flag to be set, or set or clear the flag themselves.event = threading.Event()# a client thread can wait for the flag to be setevent.wait()# a server thread can set or reset itevent.set()event.clear()If the flag is set, the wait method doesn’t do anything.If the flag is cleared, wait will block until it becomes set again.Any number of threads may wait for the same event.'''
lock
#_*_coding:utf-8_*___author__ = 'jieli'import threading,timedef run(n): time.sleep(0.5) global num lock.acquire() #申请锁并+锁 num +=1 lock.release() #释放锁if __name__ == '__main__': num = 0 lock = threading.Lock() for i in range(1000): t = threading.Thread(target=run,args=(i,)) t.start()while threading.active_count() != 1: print threading.active_count()else: print '----all threads done---' print num
Rlock
#_*_coding:utf-8_*___author__ = 'jieli'import threading,timedef run1(): print "grab the first part data" lock.acquire() global num num +=1 lock.release() return numdef run2(): print "grab the second part data" lock.acquire() global num2 num2+=1 lock.release() return num2def run3(): lock.acquire() res = run1() print '--------between run1 and run2-----' res2 = run2() lock.release() print res,res2if __name__ == '__main__': num,num2 = 0,0 lock = threading.RLock() for i in range(10): t = threading.Thread(target=run3) t.start()while threading.active_count() != 1: print threading.active_count()else: print '----all threads done---' print num
semaphore
#_*_coding:utf-8_*___author__ = 'gaogd'import threading,timedef run(n): semaphore.acquire() time.sleep(1) print "run the thread: %s\n" %n semaphore.release()if __name__ == '__main__': num= 0 semaphore = threading.BoundedSemaphore(5) ### 5个令牌,也就是同一时间可以处理5个线程 for i in range(20): t = threading.Thread(target=run,args=(i,)) t.start()while threading.active_count() >= 1: ##现在还要多少个线程在启动 pass #print threading.active_count()else: print '----all threads done---' print num'''用途:1.限制数据库的连接数'''