Study Notes (33): Python Network Programming & concurrent programming - process pool thread pool

Learning immediately: https://edu.csdn.net/course/play/24458/296451?utm_source=blogtoedu

Process pool and thread pool:

General application on the site, the largest number of processes or threads pool pools generally require as large as possible but do not exceed the carrier's server range

1. process pool:

1)concurrent.futures.ProcessPoolExecutor(n)

2) creates a process pool, a pool with n processes, tasks are handled by the n process to complete

3) n default audit the cpu

4) pool.shutdown (): means closed the entrance to the new task submission process pool, and wait for the code behind the pool after the end of the implementation process

from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor#进程池与线程池
import os,random,time
def processpool(name):
    print('pid:%s ; %s:running'%(os.getpid(),name))
    time.sleep(random.randint(2,5))

if __name__ == '__main__':
    pool = ProcessPoolExecutor(5)#创建一个进程池,池子里面有5个进程
    for i in range(10):
        pool.submit(processpool,'任务%s'%i)
    pool.shutdown(wait=True)
    print('zhu')

The result: From the results, pid process only five, 10 task has been done by these five processes

'''
"F:\software install\python3.6.4\python.exe" C:/Users/jinlin/Desktop/python_further_study/并发编程/进程池线程池.py
pid:11848 ; 任务0:running
pid:13740 ; 任务1:running
pid:12848 ; 任务2:running
pid:7196 ; 任务3:running
pid:10884 ; 任务4:running
pid:11848 ; 任务5:running
pid:13740 ; 任务6:running
pid:12848 ; 任务7:running
pid:7196 ; 任务8:running
pid:10884 ; 任务9:running
zhu

进程已结束,退出代码0

'''

2. Thread Pool

1)concurrent.futures.ThreadPoolExecutor(n)

2) creates a thread pool, the pool contains n threads, tasks are handled by these n threads to finish

3) pool.shutdown (): means closed the entrance to submit new tasks to the thread pool, and wait for the code behind after the end of the implementation of the thread pool

from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor#进程池与线程池
from threading import currentThread
import os,random,time
def processpool(name):
     print('%s pid:%s ; %s:running'%(currentThread().getName(),os.getpid(),name))
    time.sleep(random.randint(2,5))

if __name__ == '__main__':
    pool = ThreadPoolExecutor(5)#创建一个进程池,池子里面有5个进程
    for i in range(10):
        pool.submit(processpool,'任务%s'%i)
    pool.shutdown()
    print('zhu')

The result: you can know from the results, all pid thread pool threads are the same, it is because the threads in the pool are in the same process, and therefore the same pid will, and only five threads on a mission

'''
"F:\software install\python3.6.4\python.exe" C:/Users/jinlin/Desktop/python_further_study/并发编程/进程池线程池.py
ThreadPoolExecutor-0_0 pid:11888 ; 任务0:running
ThreadPoolExecutor-0_1 pid:11888 ; 任务1:running
ThreadPoolExecutor-0_2 pid:11888 ; 任务2:running
ThreadPoolExecutor-0_3 pid:11888 ; 任务3:running
ThreadPoolExecutor-0_4 pid:11888 ; 任务4:running
ThreadPoolExecutor-0_3 pid:11888 ; 任务5:running
ThreadPoolExecutor-0_2 pid:11888 ; 任务6:running
ThreadPoolExecutor-0_1 pid:11888 ; 任务7:running
ThreadPoolExecutor-0_4 pid:11888 ; 任务8:running
ThreadPoolExecutor-0_0 pid:11888 ; 任务9:running
zhu

进程已结束,退出代码0


'''

 

Published 49 original articles · won praise 11 · views 566

Guess you like

Origin blog.csdn.net/qq_45769063/article/details/105100993