A. Python multi-process lock multi-process shared memory

Source: Internet
Author: User

Lock Component

When we use multi-process to read and write files, if a process is to write a file, a process is to read the file,

If two files are at the same time, it must be impossible to read after the file has been written.

Or when multiple processes share some resources, and only one process is accessible, the lock mechanism is required to control it.

Demand:

A process to write a file, a process to append files, a process to read the file, while starting up

We can do this through the join () method of the process, which is a method that is implemented by this lock (process lock).

Function Description:

# lock = multiprocessing. Lock ()

# Lock.acquire ()???????? #获得锁

# lock.release ()???????? #释放锁




Write an unlocked program first:

#不加锁

# number? +1

# number? +3

Import?multiprocessingimport?timedef?add (number,value,lock):???? Print? ("Init?add{0}?number?=? {1} ". Format (value,?number))???? For?i?in?xrange (1,?6):???????? Number?+=?value???????? Time.sleep (1)???????? Print? ("Add{0}?number?=? {1} ". Format (value,?number))???????? if?__name__?==? " __main__ ":???? Lock?=?multiprocessing. Lock ()???? Number?=?0???? P1?=?multiprocessing. Process (target=add,args= (number,?1,?lock))???? P2?=?multiprocessing. Process (target=add,args= (number,?3,?lock))???? P1.start ()???? P2.start ()???? Print? ("Main?end")

Results:

Main?endinit?add1?number?=?0init?add3?number?=?0add1?number?=?1add3?number?=?3add1?number?=?2add3?number?=? 6add1?number?=?3add3?number?=?9add1?number?=?4add3?number?=?12add1?number?=?5add3?number?=?15


Then write the lock program:

Import?multiprocessingimport?timedef?add (number,value,lock):???? With?lock:???? Print? ("Init?add{0}?number?=? {1} ". Format (value,?number))???? For?i?in?xrange (1,?6):???????? Number?+=?value???????? Time.sleep (1)???????? Print? ("Add{0}?number?=? {1} ". Format (value,?number))???????? if?__name__?==? " __main__ ":???? Lock?=?multiprocessing. Lock ()???? Number?=?0???? P1?=?multiprocessing. Process (target=add,args= (number,?1,?lock))???? P2?=?multiprocessing. Process (target=add,args= (number,?3,?lock))???? P1.start ()???? P2.start ()???? Print? ("Main?end")

Results:

MAIN?ENDINIT?ADD1?NUMBER?=?0#ADD1 priority to grab the lock, priority to execute ADD1?NUMBER?=?1ADD1?NUMBER?=?2ADD1?NUMBER?=?3ADD1?NUMBER?=?4ADD1? Number?=?5init?add3?number?=?0#add3 is blocked, waiting for add1 execution to complete, releasing the lock after executing ADD3ADD3?NUMBER?=?3ADD3?NUMBER?=?6ADD3?NUMBER?=?9ADD3? Number?=?12add3?number?=?15



Using Lock.acquire () and Lock.release ()

Import?multiprocessingimport?timedef?add (number,value,lock):???? Lock.acquire ()???? Try:???????? Print? ("Init?add{0}?number?=? {1} ". Format (value,?number))???????? For?i?in?xrange (1,?6):???????????? Number?+=?value???????????? Time.sleep (1)???????????? Print? ("Add{0}?number?=? {1} ". Format (value,?number))???? Except? Exception?as?e:???????? Raise?e???? Finally:???????? Lock.release () if?__name__?==? " __main__ ":???? Lock?=?multiprocessing. Lock ()???? Number?=?0???? P1?=?multiprocessing. Process (target=add,args= (number,?1,?lock))???? P2?=?multiprocessing. Process (target=add,args= (number,?3,?lock))???? P1.start ()???? P2.start ()???? Print? ("Main?end")

Results:

Main?endinit?add1?number?=?0add1?number?=?1add1?number?=?2add1?number?=?3add1?number?=?4add1?number?=?5init? Add3?number?=?0add3?number?=?3add3?number?=?6add3?number?=?9add3?number?=?12add3?number?=?15



Shared memory

Python's multiprocessing module also provides us with shared memory operations.

General variables are not able to communicate between processes, multiprocessing provides us with the Value and Array modules, they can be used in the process of non-pass together


Example: Do not lock, let number after 1 and then continue to add 3, and then continue to add one, and then continue to add 3 ...

Import?multiprocessingimport?timedef?add (number,add_value):???? Try:???????? Print? ("Init?add{0}?number?=? {1} ". Format (add_value,?number.value))???????? For?i?in?xrange (1,?6):???????????? Number.value?+=?add_value???????????? Print? ("***************add{0}?has?added***********". Format (add_value))???????????? Time.sleep (1)???????????? Print? ("Add{0}?number?=? {1} ". Format (add_value,?number.value))???? Except? Exception?as?e:???????? Raise?e???????? if?__name__?==? " __main__ ":???? Number?=?multiprocessing. Value (' i ',? 0)???? P1?=?multiprocessing. Process (target=add,args= (number,?1))???? P2?=?multiprocessing. Process (target=add,args= (number,?3))???? P1.start ()???? P2.start ()???? Print? ("Main?end")

Printing results:

Main?endinit?add1?number?=?0***************add1?has?added***********init?add3?number?=?1***************add3? Has?added***********add1?number?=?4***************add1?has?added***********add3?number?=?5***************add3? Has?added***********add1?number?=?8***************add1?has?added***********add3?number?=?9***************add3? has?added***********add1?number?=?12***************add1?has?added***********add3?number?=?13*************** add3?has?added***********add1?number?=?16***************add1?has?added***********add3?number?=?17************* **add3?has?added***********add1?number?=?20add3?number?=?20



To add a lock again:

Add 1 to the process and then execute plus 3.

Import?multiprocessingimport?timedef?add (number,add_value,lock):???? Lock.acquire ()???? Try:???????? Print? ("Init?add{0}?number?=? {1} ". Format (add_value,?number.value))???????? For?i?in?xrange (1,?6):???????????? Number.value?+=?add_value???????????? Print? ("***************add{0}?has?added***********". Format (add_value))???????????? Time.sleep (1)???????????? Print? ("Add{0}?number?=? {1} ". Format (add_value,?number.value))???? Except? Exception?as?e:???????? Raise?e???? Finally:???????? Lock.release ()???????? if?__name__?==? " __main__ ":???? Lock?=?multiprocessing. Lock ()???? Number?=?multiprocessing. Value (' i ',? 0)???? P1?=?multiprocessing. Process (target=add,args= (number,?1,?lock))???? P2?=?multiprocessing. Process (target=add,args= (number,?3,?lock))???? P1.start ()???? P2.start ()???? Print? ("Main?end")

Execution results (compare above):

Main?endinit?add1?number?=?0***************add1?has?added***********add1?number?=?1***************add1?has? Added***********add1?number?=?2***************add1?has?added***********add1?number?=?3***************add1?has? added***********add1?number?=?4***************add1?has?added***********add1?number?=?5init?add3?number?=?5**** add3?has?added***********add3?number?=?8***************add3?has?added***********add3?number?=?11*** Add3?has?added***********add3?number?=?14***************add3?has?added***********add3?number?=?17* Add3?has?added***********add3?number?=?20


Multi-process shared memory implementation:

Import?multiprocessingimport?timedef?add (number,add_value,lock):???? Lock.acquire ()???? Try:???????? Print? ("Init?add{0}?number?=? {1} ". Format (add_value,?number.value))???????? For?i?in?xrange (1,?6):???????????? Number.value?+=?add_value???????????? Print? ("***************add{0}?has?added***********". Format (add_value))???????????? Time.sleep (1)???????????? Print? ("Add{0}?number?=? {1} ". Format (add_value,?number.value))???????? Except? Exception?as?e:???????????? Raise?e???????? Finally:???????????? Lock.release () Def?change (arr):???? For?i?in?range (Len (arr)):???????? Arr[i]?=?-arr[i]???????? if?__name__?==? " __main__ ":???? Lock?=?multiprocessing. Lock ()???? Number?=?multiprocessing. Value (' i ',? 0)???? Arr?=?multiprocessing. Array (' i ',? Range ())???? Print? (arr[:])???? P1?=?multiprocessing. Process (target=add,args= (number,?1,?lock))???? P2?=?multiprocessing. Process (target=add,args= (number,?3,?lock))???? P3?=?multiprocessing. Process (target=change,args= (arr))???? P1.start ()???? P2.start ()???? P3.start ()???? P3.join ()???? PriNt? (arr[:])???? Print? ("Main?end")

Results:

[0,?1,?2,?3,?4,?5,?6,?7,?8,?9]init?add3?number?=?0***************add3?has?added***********[0,?-1,?-2,?-3,?-4,? " -5,?-6,?-7,?-8,?-9]main?endadd3?number?=?3***************add3?has?added***********add3?number?=?6************* **add3?has?added***********add3?number?=?9***************add3?has?added***********add3?number?=?12************ Add3?has?added***********add3?number?=?15init?add1?number?=?15***************add1?has?added***********add1? number?=?16***************add1?has?added***********add1?number?=?17***************add1?has?added*********** add1?number?=?18***************add1?has?added***********add1?number?=?19***************add1?has?added********* **add1?number?=?20


A. Python multi-process lock multi-process shared memory

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.