The Python standard library provides us with the threading and multiprocessing modules to write the corresponding multithreaded/multi-process code, but when the project reaches a certain scale, frequent creation/destruction of processes or threads is very resource-intensive, At this point we are going to write our own pool of threads/processes to change the time in space. But starting with Python3.2, the standard library provides us with the Concurrent.futures module, which provides Threadpoolexecutor and processpoolexecutor two classes, enabling the threading and multiprocessing One-step abstraction that provides direct support for writing thread pool/process pools.
module: Threadpoolexecutor,processpoolexecutor Subclass of Concurrent.futures module
Simple example:
From concurrent.futures Import threadpoolexecutor,processpoolexecutorimport timedef Task (ARG1,ARG2): print (arg1 , arg2) time.sleep (1) # pool = Processpoolexecutor (ten) pool = Threadpoolexecutor (ten) for I in range: Pool.submit (Task,i,i)
Python3 "Modules" Concurrent.futures modules, thread pool process pools