day34-python之进程调用
程序员文章站
2022-03-20 21:08:16
1.信号量 2.同步对象 3.生产者消费者模型 4.递归锁 5.进程调用 ......
1.信号量
import threading,time class mythread(threading.thread): def run(self): if semaphore.acquire(): print(self.name) time.sleep(3) semaphore.release() if __name__=="__main__": semaphore=threading.semaphore() thrs=[] for i in range(100): thrs.append(mythread()) for t in thrs: t.start()
2.同步对象
import threading,time class boss(threading.thread): def run(self): print("boss:今晚大家都要加班到22:00。") print(event.isset())# false event.set() time.sleep(5) print("boss:<22:00>可以下班了。") print(event.isset()) event.set() class worker(threading.thread): def run(self): event.wait()# 一旦event被设定,等同于pass print("worker:哎……命苦啊!") time.sleep(1) event.clear() event.wait() print("worker:ohyeah!") if __name__=="__main__": event=threading.event() threads=[] for i in range(5): threads.append(worker()) threads.append(boss()) for t in threads: t.start() for t in threads: t.join() print("ending.....")
3.生产者消费者模型
# import time,random # import queue,threading # # q = queue.queue() # # def producer(name): # count = 0 # while count <10: # print("making........") # time.sleep(5) # q.put(count) # print('producer %s has produced %s baozi..' %(name, count)) # count +=1 # #q.task_done() # q.join() # print("ok......") # def consumer(name): # count = 0 # while count <10: # time.sleep(random.randrange(4)) # # if not q.empty(): # # print("waiting.....") # #q.join() # data = q.get() # print("eating....") # time.sleep(4) # # q.task_done() # #print(data) # print('\033[32;1mconsumer %s has eat %s baozi...\033[0m' %(name, data)) # # else: # # print("-----no baozi anymore----") # count +=1 # # p1 = threading.thread(target=producer, args=('a君',)) # c1 = threading.thread(target=consumer, args=('b君',)) # c2 = threading.thread(target=consumer, args=('c君',)) # c3 = threading.thread(target=consumer, args=('d君',)) # # p1.start() # c1.start() # c2.start() # c3.start()
4.递归锁
import threading import time class mythread(threading.thread): def actiona(self): r_lcok.acquire() #count=1 print(self.name,"gota",time.ctime()) time.sleep(2) r_lcok.acquire() #count=2 print(self.name, "gotb", time.ctime()) time.sleep(1) r_lcok.release() #count=1 r_lcok.release() #count=0 def actionb(self): r_lcok.acquire() print(self.name, "gotb", time.ctime()) time.sleep(2) r_lcok.acquire() print(self.name, "gota", time.ctime()) time.sleep(1) r_lcok.release() r_lcok.release() def run(self): self.actiona() self.actionb() if __name__ == '__main__': # a=threading.lock() # b=threading.lock() r_lcok=threading.rlock() l=[] for i in range(5): t=mythread() t.start() l.append(t) for i in l: i.join() print("ending....")
5.进程调用
# from multiprocessing import process # import time # # # def f(name): # time.sleep(1) # print('hello', name,time.ctime()) # # if __name__ == '__main__': # p_list=[] # for i in range(3): # # p = process(target=f, args=('alvin',)) # p_list.append(p) # p.start() # # for i in p_list: # i.join() # print('end') from multiprocessing import process import time # class myprocess(process): # # # def __init__(self): # # super(myprocess, self).__init__() # # #self.name = name # # def run(self): # time.sleep(1) # print ('hello', self.name,time.ctime()) # # if __name__ == '__main__': # p_list=[] # # # for i in range(3): # p = myprocess() # p.daemon=true # p.start() # p_list.append(p) # # # for p in p_list: # # p.join() # # print('end') # from multiprocessing import process # import os # import time # # # def info(title): # print("title:", title) # print('parent process:', os.getppid()) # print('process id:', os.getpid()) # # def f(name): # # info('function f') # print('hello', name) from multiprocessing import process import os import time def info(title): print("title",title) print("parent process:",os.getppid()) print("process id:",os.getpid()) def f(name): info('function f') print("hello",name) if __name__ == '__main__': info('main process line') time.sleep(1) print("-------") p = process(target=info("yuan")) p.start() p.join() # if __name__ == '__main__': # # info('main process line') # # time.sleep(1) # print("------------------") # p = process(target=info, args=('yuan',)) # p.start() # p.join()