Recently, Python is used to process the public image database. Due to the large amount of data, it takes too long to process the images one by one in serial. Therefore, it is decided to adopt the parallel mode to make full use of the CPU on the host to speed up the processing process, which can greatly reduce the total processing time.
Here we use the concurrent.futures Module, which can use multiprocessing to achieve real parallel computing.
The core principle is: concurrent.futures It will run multiple Python interpreters in parallel in the form of sub processes, so that Python programs can use multi-core CPU to improve the execution speed. Since the subprocess is separated from the main interpreter, their global interpreter locks are also independent of each other. Each subprocess can use a CPU core completely.
The specific implementation is also very simple, the code is as follows. How many CPU cores the host has will start how many Python processes for parallel processing.
import concurrent.futures
def function(files):
# To do what you want
# files: file list that you want to process
if __name__ == '__main__':
with concurrent.futures.ProcessPoolExecutor() as executor:
executor.map(function, files)
After changing to parallel processing, my 12 CPUs run at full load, and the processing speed is significantly faster.
div>
Read More:
- Asynchronous Future Parallel processing request code example
- Unable to boot : please use a kernel appropriate for your cpu
- Installation and use of XmR stak CPU
- Java uses lock to realize multithread sequential alternate execution mode 2
- Python conversion hex to string, high and low data processing
- How to solve oserror: [errno 98] address already in use and kill the python3 process
- ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full comm
- Command errored out with exit status 1: python setup.py egg_info Check the logs for full command ou
- Centos7 quick installation of docker and configuration of image acceleration
- How to realize automatic assembly in springboot
- emulator: ERROR: x86 emulation currently requires hardware acceleration! Abnormal problem solved
- Using xrdp to realize windows remote desktop Ubuntu Linux
- Learn English together | three ways to realize digital factorial with JavaScript
- GPU hardware acceleration related problems, solve flash screen
- Problems in the construction of CUDA environment (GPU parallel programming)
- Deploy mongodb fragment combined with replica set to realize distributed storage of MySQL database files (step 10)
- Use Python to insert the specified content in the specified line of the specified file
- Detailed steps for installing Visual Studio 2010 + Intel parallel studio Xe 2013 and configuring mpich2
- How to use Python split() function (split array)
- Running Ubuntu 16.04 in VMware to solve the full screen problem