Python线程模块threading
Threading.Condition:
import random,time import threading from threading import Condition class Producer(threading.Thread): """ Produces random integers to a list """ def __init__(self, integers, condition): """ Constructor. @param integers list of integers @param condition condition synchronization object """ threading.Thread.__init__(self) self.integers = integers self.condition = condition print("threading-1 created") def run(self): """ Thread run method. Append random integers to the integers list at random time. """ while True: integer = random.randint(0, 256) self.condition.acquire() print('condition acquired by %s' % self.name) self.integers.append(integer) print ('%d appended to list by %s' % (integer, self.name)) print ('condition notified by %s' % self.name) self.condition.notify() print( 'condition released by %s' % self.name) self.condition.release() time.sleep(1) class Consumer(threading.Thread): """ Consumes random integers from a list """ def __init__(self, integers, condition): """ Constructor. @param integers list of integers @param condition condition synchronization object """ threading.Thread.__init__(self) self.integers = integers self.condition = condition print("threading-2 created") def run(self): """ Thread run method. Consumes integers from list """ while True: print("thread2 befor acquire") self.condition.acquire() print( 'condition acquired by %s' % self.name) while True: if self.integers: integer = self.integers.pop() print( '%d popped from list by %s' % (integer, self.name)) break print ('condition wait by %s' % self.name) self.condition.wait() print("thread2 after wait") print ('condition released by %s' % self.name) self.condition.release() def main(): integers = [] condition = threading.Condition() t1 = Producer(integers, condition) t2 = Consumer(integers, condition) t1.start() print("t1 started") t2.start() print("t2 started") time.sleep(3) for i in range(10): print("fuck") t1.join() t2.join() if __name__ == '__main__': main()
Threading.Semaphore:
from threading import Thread,Semaphore import threading,time,random class t_S_P(Thread): def __init__(self,integer,Sema): Thread.__init__(self) self.integer=integer self.Sema=Sema def run(self): while True: a=random.randint(0,100) self.integer.append(a) print("%d was add to the list by Thread P"%a) self.Sema.acquire() class t_S_C(Thread): def __init__(self,integer,Sema): Thread.__init__(self) self.integer = integer self.Sema=Sema def run(self): while True: print("fuck") if self.integer: p=self.integer.pop() print("%d was popped by Thread C"%p) self.Sema.release() time.sleep(1) if __name__=="__main__": integer=[] Sema=Semaphore() p=t_S_P(integer,Sema) c=t_S_C(integer,Sema) p.start() c.start() p.join() c.join()
Threading.Barrier:
import threading,random from threading import Barrier,Thread from threading import Condition,Lock class Barrier_sum(Barrier): def __init__(self, parties, action=None, timeout=None): super().__init__(parties,action=action,timeout=timeout) self.args = (0, 0, 0) def _release(self): try: if self._action: self._action(*self.args) # enter draining state self._state = 1 self._cond.notify_all() except: # an exception during the _action handler. Break and reraise self._break() raise class t_B(Thread): def __init__(self,barrier,d_list): Thread.__init__(self) self.barrier=barrier self.d_list=d_list def run(self): #integer=random.randint(0,10) l_integer=random.randint(0,2) self.d_list[l_integer] =self.d_list[l_integer]+1 print(self.d_list) self.barrier.args=(self.d_list[0],self.d_list[1],self.d_list[2]) self.barrier.wait() print("%d quite"%threading.get_ident()) if __name__=="__main__": def fuck(a,b,c): print("sum value is %d"%(a+b+c)) return a+b+c b_list = [1, 2, 3] t_list=[] barrier=Barrier_sum(3,action=fuck) for i in range(36): t_list.append(t_B(barrier,b_list)) for b in t_list: b.start() for b in t_list: b.join()
Threading.Event:
from threading import Event from threading import Thread import random,time class t_E_P(Thread): def __init__(self,integer,event): Thread.__init__(self) self.integer=integer self.event=event def run(self): while True: a=random.randint(0,100) self.integer.append(a) print("%d was add to the list by Thread P"%a) self.event.set() self.event.clear() print("cleared by Thread P now,flase") time.sleep(1) class t_E_C(Thread): def __init__(self,integer,event): Thread.__init__(self) self.integer = integer self.event = event def run(self): while True: print("fuck") if self.integer: p=self.integer.pop() print("%d was popped by Thread C"%p) self.event.wait() if __name__=="__main__": integer=[] event=Event() p=t_E_P(integer,event) c=t_E_C(integer,event) p.start() c.start() p.join() c.join()
Queue:
from queue import Queue from threading import Thread import random,time class Procuder(Thread): def __init__(self,queue): Thread.__init__(self) self.queue=queue self.i=50 def run(self): while self.i>0: value=random.randint(0,10) self.queue.put(value) print("%d was put in to "%value) #print("all been get") self.i-=1 self.queue.join() print("thread p done") class Consumer(Thread): def __init__(self, queue): Thread.__init__(self) self.queue = queue def run(self): while True: value=self.queue.get() time.sleep(0.005) print("%d get "%value) #print("unfinished %d"%self.queue.unfinished_tasks) self.queue.task_done() if self.queue.unfinished_tasks==0: #print("fuck") break if __name__=="__main__": q=Queue() p=Procuder(q) c=Consumer(q) p.start() c.start()