Is multi-processing or multi-threading faster in python?
The following editor will bring you an article on who is faster between python multi-process and multi-thread (detailed explanation). The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.
python3.6
threading and multiprocessing
##Quad-core + Samsung 250G-850-SSD
Since used For multi-process and multi-thread programming, I still don’t understand which one is faster. Many people on the Internet say that Python multi-process is faster because of GIL (Global Interpreter Lock). But when I write code, the test time is faster with multi-threading, so what is going on? Recently I have been doing word segmentation work again. The original code is too slow and I want to speed it up, so let’s explore effective methods (there are codes and renderings at the end of the article)Here is the result of the program. Diagram to illustrate which thread or process is faster
Some definitions
Parallelism means two or more events occur at the same time. Concurrency means that two or more events occur within the same time interval. Threads are the smallest units that the operating system can perform computing scheduling. It is included in the process and is the actual operating unit in the process. An execution instance of a program is a process.Implementation process
The multi-threads in python obviously have to get the GIL, execute the code, and finally release the GIL. So because of GIL, you can't get it when using multiple threads. In fact, it is a concurrent implementation, that is, multiple events occur at the same time interval. But the process has an independent GIL, so it can be implemented in parallel. Therefore, for multi-core CPUs, theoretically using multiple processes can more effectively utilize resources.Real-life problems
In online tutorials, you can often see python multi-threading. For example, web crawler tutorials and port scanning tutorials. Take port scanning as an example. You can use multi-process to implement the following script, and you will find that python multi-process is faster. So isn’t it contrary to our analysis?import sys,threading from socket import * host = "127.0.0.1" if len(sys.argv)==1 else sys.argv[1] portList = [i for i in range(1,1000)] scanList = [] lock = threading.Lock() print('Please waiting... From ',host) def scanPort(port): try: tcp = socket(AF_INET,SOCK_STREAM) tcp.connect((host,port)) except: pass else: if lock.acquire(): print('[+]port',port,'open') lock.release() finally: tcp.close() for p in portList: t = threading.Thread(target=scanPort,args=(p,)) scanList.append(t) for i in range(len(portList)): scanList[i].start() for i in range(len(portList)): scanList[i].join()
Who is faster
Because of the python lock problem, threads competing for locks and switching threads will consume resources. So, let’s take a bold guess: In CPU-intensive tasks, multi-processing is faster or more effective; while in IO-intensive tasks, multi-threading can effectively improve efficiency. Let’s take a look at the following code:import time import threading import multiprocessing max_process = 4 max_thread = max_process def fun(n,n2): #cpu密集型 for i in range(0,n): for j in range(0,(int)(n*n*n*n2)): t = i*j def thread_main(n2): thread_list = [] for i in range(0,max_thread): t = threading.Thread(target=fun,args=(50,n2)) thread_list.append(t) start = time.time() print(' [+] much thread start') for i in thread_list: i.start() for i in thread_list: i.join() print(' [-] much thread use ',time.time()-start,'s') def process_main(n2): p = multiprocessing.Pool(max_process) for i in range(0,max_process): p.apply_async(func = fun,args=(50,n2)) start = time.time() print(' [+] much process start') p.close()#关闭进程池 p.join()#等待所有子进程完毕 print(' [-] much process use ',time.time()-start,'s') if name=='main': print("[++]When n=50,n2=0.1:") thread_main(0.1) process_main(0.1) print("[++]When n=50,n2=1:") thread_main(1) process_main(1) print("[++]When n=50,n2=10:") thread_main(10) process_main(10)
The results are as follows:
CPU and IO intensive
1, CPU intensive code (various loop processing, counting, etc.)Judgment method:
1. Directly look at the CPU usage, hard disk IO read and write speedExamples of multi-process and multi-threading in Python (1)
2.Recommended to use multi-process instead of multi-threading in Python? Share the reasons for recommending the use of multi-process
3.Examples of multi-process and multi-threading in Python (2) Programming methods
4.About Python Detailed introduction to processes, threads, and coroutines
5.Python concurrent programming thread pool/process pool
The above is the detailed content of Is multi-processing or multi-threading faster in python?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
