#多線程 from concurrent.futures import ThreadPoolExecutor,as_completed from concurrent.futures import ProcessPoolExecutor import time def fib(n): if n <= 2: return 1 return fib(n-1)+fib(n-2) with ThreadPoolExecutor(3) as excutor: all_task=[excutor.submit(fib,(num)) for num in range(25,35)] start_time=time.time() for future in as_completed(all_task): data=future.result() print("result:{}".format(data)) end_time=time.time() print("last time : {}".format(end_time-start_time)) #output: result:75025 result:121393 result:196418 result:317811 result:514229 result:832040 result:1346269 result:2178309 result:3524578 result:5702887 last time : 98.66604399681091
#多進程 from concurrent.futures import ThreadPoolExecutor,as_completed from concurrent.futures import ProcessPoolExecutor import time def fib(n): if n <= 2: return 1 return fib(n-1)+fib(n-2) if __name__ == "__main__": with ProcessPoolExecutor(3) as excutor: all_task = [excutor.submit(fib, (num)) for num in range(25, 35)] start_time = time.time() for future in as_completed(all_task): data = future.result() print("result:{}".format(data)) end_time = time.time() print("last time : {}".format(end_time - start_time)) #output: result:75025 result:121393 result:196418 result:317811 result:514229 result:832040 result:1346269 result:2178309 result:3524578 result:5702887 last time : 14.470988988876343
#input from concurrent.futures import ProcessPoolExecutor import multiprocessing #多進程編程 import time def get_html(n): time.sleep(n) print("sub_progress sccess") if __name__=="__main__": progress = multiprocessing.Process(target=get_html,args=(3,)) print(progress.pid) progress.start() print(progress.pid) progress.join() #output None 12864 sub_progress sccess
import multiprocessing #多進程編程 import time class progress_get_html(multiprocessing.Process): def __init__(self,n): self.n=n super().__init__() def run(self): time.sleep(self.n) print("sub progress success") class MyProgress(multiprocessing.Process): def __init__(self,n): self.n=n super().__init__() def run(self): pro=progress_get_html(self.n) pro.start() print("progress end") if __name__=="__main__": progress = MyProgress(3) print(progress.pid) progress.start() print(progress.pid) progress.join() #output: None 8744 progress end sub progress success
from concurrent.futures import ProcessPoolExecutor from multiprocessing import pool import multiprocessing #多進程編程 import time def get_html(n): time.sleep(n) print("sub_progress sccess") return n if __name__=="__main__": pool=multiprocessing.Pool(multiprocessing.cpu_count()) result=pool.apply_async(get_html,args=(3,)) print(result.get()) #pool在調用join以前 須要調用close 來讓它再也不接收任務。不然會報錯 pool.close() pool.join() print(result.get()) #output sub_progress sccess 3 3
其餘方法:
if __name__=="__main__": pool=multiprocessing.Pool(multiprocessing.cpu_count()) for result in pool.imap(get_html,[1,5,3]): print("sleep {} successed ".format(result)) #output: sub_progress sccess sleep 1 successed sub_progress sccess sub_progress sccess sleep 5 successed sleep 3 successed
if __name__=="__main__": pool=multiprocessing.Pool(multiprocessing.cpu_count()) #for result in pool.imap(get_html,[1,5,3]): # print("sleep {} successed ".format(result)) for result in pool.imap_unordered(get_html,[1,5,3]): print("sleep {} successed ".format(result)) #output: sub_progress sccess sleep 1 successed sub_progress sccess sleep 3 successed sub_progress sccess sleep 5 successed
from multiprocessing import Process,Queue import time def producer(queue): queue.put("a") time.sleep(2) def consumer(queue): time.sleep(2) data=queue.get() print(data) if __name__== "__main__": queue=Queue(10) my_producer = Process(target=producer,args=(queue,)) my_consumer = Process(target=consumer,args=(queue,)) my_producer.start() my_consumer.start() my_producer.join() my_consumer.join() #outpu: a
from multiprocessing import Process,pool,Manager,Pool import time def producer(queue): queue.put("a") time.sleep(2) def consumer(queue): time.sleep(2) data=queue.get() print(data) if __name__== "__main__": queue=Manager().Queue() pool=Pool(3) pool.apply_async(producer,args=(queue,)) pool.apply_async(consumer,args=(queue,)) pool.close() pool.join() #output: a
from multiprocessing import Process,pool,Manager,Pool,Pipe import time def producer(pipe): pipe.send("hello") def consumer(pipe): print(pipe.recv()) if __name__== "__main__": recv_pipe,send_pipe=Pipe() my_producer=Process(target=producer,args=(send_pipe,)) my_consumer=Process(target=consumer,args=(recv_pipe,)) my_producer.start() my_consumer.start() my_producer.join() my_consumer.join() #output: hello
from multiprocessing import Process,pool,Manager,Pool,Pipe import time def add_data(p_dict,key,value): p_dict[key]=value if __name__ == "__main__": progress_dict= Manager().dict() first_progress= Process(target=add_data,args=(progress_dict,"name","tangrong")) second_progress = Process(target=add_data,args=(progress_dict,"age","18")) first_progress.start() second_progress.start() first_progress.join() second_progress.join() print(progress_dict) #output: {'name': 'tangrong', 'age': '18'}