线程简单写法:
import threading,time def run(n): time.sleep(2) print("task",n) # # t1 = threading.Thread(target=run,args=(10,"t1")) # t2 = threading.Thread(target=run,args=(3,"t2")) # t1.start() # t2.start() start_time = time.time() for i in range(50): t = threading.Thread(target=run,args=(i,)) t.start() exit("exit!")
线程等待:
import threading,time class MyThread(threading.Thread): def __init__(self, n,sleep_time): super(MyThread,self).__init__() self.n = n self.sleeptime = sleep_time def run(self): print("running task", self.n) time.sleep(self.sleeptime) print("task done",self.n) t1 = MyThread("t1",2) t2 = MyThread("t2",4) start_time = time.time() t1.start() t1.join() t2.start() t2.join() end_time = time.time() ptime = end_time - start_time print(ptime)
并发:
def run(n): time.sleep(2) print("task",n) start_time = time.time() t_objs = [] for i in range(50): t = threading.Thread(target=run,args=('t-%s'%i,)) t.start() t_objs.append(t) for t in t_objs: t.join() print("---------------all thread has finished...") print("cost:",time.time()-start_time)
设置守护进程:
import threading,time def run(n): time.sleep(2) print("task done",n,threading.current_thread()) start_time = time.time() t_objs = [] for i in range(50): t = threading.Thread(target=run,args=('t-%s'%i,)) t.setDaemon(True) #把当前线程设置成守护线程,需要在start前设置 t.start() t_objs.append(t) # for t in t_objs: # t.join() print("---------------all thread has finished...",threading.current_thread()) print("cost:",time.time()-start_time)
递归锁:
import threading import time def run1(): print("grad the first part data") lock.acquire() global num num += 1 lock.release() return num def run2(): print("grab the second part data") lock.acquire() global num2 num2 += 1 lock.release() return num2 def run3(): lock.acquire() res = run1() print("-------------between run1 and run2----------") res2 = run2() lock.release() print(res, res2) 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, num2)
信号量:
import threading import time def run(n): semaphore.acquire() time.sleep(1) print("run the thread:%s " % n) semaphore.release() if __name__ == '__main__': semaphore = threading.BoundedSemaphore(5) #设定了最大5个线程同时运行 for i in range(22): t = threading.Thread(target=run, args=(i,)) t.start() while threading.active_count() != 1: pass else: print("-----------all threads done----------")
event:
import time import threading event = threading.Event() def lighter(): count = 0 event.set() while True: if count > 5 and count <= 10: #改成红灯 event.clear() print(" 33[41;1mred light is on .... 33[0m") elif count > 10: event.set() #变绿灯 count = 0 else: print(" 33[42;1mgreen light is on.... 33[0m") time.sleep(1) count += 1 print(count) def car(name): while True: if event.is_set(): print("[%s] running..." % name) else: print("[%s] sees red light , waiting..." % name) event.wait() print(" 33[34;1m[%s] green light is on, start going... 33[0m" % name) time.sleep(1) light = threading.Thread(target=lighter,) light.start() car1 = threading.Thread(target=car, args=("Tesla",)) car1.start()