python 多进程,多线程
python 多进程,多线程
import time from math import sqrt def test_func_1(val): time.sleep(1) return sqrt(val**2) %time [test_func_1(i) for i in range(10)] #多进程 from joblib import Parallel,delayed %time Parallel(n_jobs=2)(delayed(test_func_1)(i) for i in range(10)) #多进程 from concurrent.futures import ProcessPoolExecutor from multiprocessing import Process,current_process def task(i): print(f'{current_process().name} 在执行任务{i}') time.sleep(1) if __name__ == '__main__': pool = ProcessPoolExecutor(4) # 进程池里又4个进程 for i in range(20): # 20个任务 # print(i) pool.submit(task,i)# 进程池里当前执行的任务i,池子里的4个进程一次一次执行任务 # 多线程 from concurrent.futures import ThreadPoolExecutor from threading import Thread,currentThread import time def task(i): print(f'{currentThread().name} 在执行任务{i}') time.sleep(1) if __name__ == '__main__': pool = ThreadPoolExecutor(4) # 进程池里又4个线程 for i in range(20): # 20个任务 pool.submit(task,i)# 线程池里当前执行的任务i,池子里的4个线程一次一次执行任务 from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor import os,time,random def task(n): print('%s is runing' %os.getpid()) time.sleep(random.randint(1,3)) return n**2 if __name__ == '__main__': executor=ThreadPoolExecutor(max_workers=3) # for i in range(20): # future=executor.submit(task,i) executor.map(task,range(1,21)) #map取代了for+submit
多线程 - 条件锁
题目:有几个生产车间生产,几个消费者购买,当生产达到一定数量时,停止生产。
import threading import time condtion = threading.Condition() sheep = ['1件产品','1件产品','1件产品','1件产品','1件产品'] class Producer(threading.Thread): def __init__(self, name): super().__init__(name=name) pass def run(self): global condtion, sheep while True: time.sleep(0.1) condtion.acquire() #调用关联锁的方法 if len(sheep) < 10: print(self.name + "生产了1件产品") sheep.append('1件产品') #~notify():从等待池挑选一个线程并通知,收到通知的线程将自动调用 acquire() 尝试获得, #其他线程仍然在等待池中等待通知,直到该线程收到通知 调用该方法,否则将会抛出异常。 #~notify ALL():跟notify() 一样,但这个方法对应的是所有的线程。 condtion.notifyAll() pass else: print("仓库满了,停止生产!") #使线程进入 Condition 的等待池等待通知并释放解锁。 #使用前线程必须已获得锁定,否则将抛出异常。 condtion.wait() pass condtion.release() #解锁 pass pass class Customer(threading.Thread): def __init__(self, name): super().__init__(name=name) pass def run(self): global condtion, sheep while True: time.sleep(0.1) condtion.acquire() if len(sheep) > 0: meat = sheep.pop() print(self.name + "购买了" + meat + "还剩多少" + str(len(sheep)) + "件") condtion.notifyAll() pass else: print("买光了,等待") condtion.wait() pass condtion.release() pass pass if __name__ == "__main__": p1 = Producer("1号生产车间") p2 = Producer("2号生产车间") p3 = Producer("3号生产车间") p4 = Producer("4号生产车间") p5 = Producer("5号生产车间") p6 = Producer("6号生产车间") p1.start() p2.start() p4.start() p5.start() p6.start() c1 = Customer('小王') c2 = Customer('小李') c3 = Customer('小贾') c4 = Customer('小沈') c5 = Customer('小刘') c1.start() c2.start() c3.start() c4.start() c5.start()