Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Principle Analysis of Multi-process and Mutual exclusion Lock in python concurrent programming

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "the principle analysis of multi-process and mutex lock in python concurrent programming". In daily operation, I believe that many people have doubts about the principle analysis of multi-process and mutex lock in python concurrent programming. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "principle analysis of multi-process and mutex lock in python concurrent programming". Next, please follow the editor to study!

The memory space of each child process running multiple processes is isolated from each other and the data cannot be shared among the processes.

Mutex lock

However, processes run on the same operating system, and data is not shared between processes, but they share the same set of file systems, so they access the same file or the same print terminal.

Yes, but sharing brings competition, and the result of competition is confusion.

# concurrent operation, high efficiency, but competition for the same print terminal leads to printing disorder from multiprocessing import Processimport timedef task (name): print ("% s 1"% name) time.sleep (1) print ("% s 2"% name) time.sleep (1) print ("% s 3"% name) if _ name__ = ='_ main__': for i in range (3): P = Process (target=task, args= ("child process% s"% I) ) p.start ()''child process 2 1 child process 0 1 child process 1 1 child process 2 2 child process 1 2 child process 0 2 child process 2 3 child process 1 3 child process 0 3 child process

How to control is to add lock processing. And mutex means mutual exclusion. If you compare multiple processes to multiple individuals,

The principle of mutex is that multiple people have to compete for the same resource: in the bathroom, one person grabs the bathroom and puts a lock on it, and everyone else has to wait until the lock is released after the task is completed.

So the principle of mutex is to change the concurrency to serial, which reduces the efficiency, but ensures the data security and chaos.

Adding a mutex has no concurrency effect, and adding a lock only one can run the mutex will make it less efficient to turn concurrency into serial.

Resolve:

Import module Lock

Now the program starts all processes and first grabs the lock. Only those who get the lock can run.

Wait for this process to run and unlock and then go to other processes to continue to grab the lock.

From multiprocessing import Process, Lockimport timedef task (name Mutex): # Lock mutex.acquire () print ("% s 1"% name) time.sleep (1) print ("% s 2"% name) time.sleep (1) print ("% s 3"% name) # remove the lock mutex.release () if _ _ name__ ='_ main__': # create an object instance mutex = Lock () for i in range (3): # Pass the lock to the child process so that all child processes use the same lock p = Process (target=task Args= ("child process% s"% I, mutex) p.start ()''now the program starts all processes will first grab the lock, only those who grab the lock can run until this process has run and unlocked, and then continue to grab the lock to other processes' child process 0 1 child process 0 2 child process 0 3 child process 1 1 child process 1 2 child process 1 3 child process 2 1 child process 2 2 child process 2 3 child processes

At the expense of efficiency to ensure that the data is good and messy.

At this point, the study on "the principle analysis of multi-processes and mutexes in python concurrent programming" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report