(vii) 8-1 multi-process lock and shared memory

Source: Internet
Author: User

Multi-process Lock component
When we use multiple processes to read and write files, a process writes a file, and a process reads the file. If two processes at the same time, certainly not, you must wait until the completion of the write before you can do more operations. Or, when multiple processes share some resources, only one process can access them, requiring a lock mechanism control.

Import multiprocessing
Import time
Lock = multiprocessing. Lock ()
def add (Number,value,lock):
With Lock:
Print "Init add{0} number={1}". Format (Value,number)
For I in xrange (1,5):
Number + = value
Time.sleep (1)
print "add{0} num = {1}". Format (Value,number)


if __name__ = = "__main__":
Number = 0
Lock = multiprocessing. Lock ()
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"

Operation Result:

Main end
Init add1 number=0
Init add3 number=0
Add1 num = 1
Add3 num = 3
Add1 num = 2
Add3 num = 6
Add1 num = 3
Add3 num = 9
Add1 num = 4
Add3 num = 12

Lock operation with lock mode:
#!/usr/bin/env python
#coding: UTF8
# import Multiprocessing
# lock = multiprocessing. Lock ()
# Lock.acquire () Get lock
# lock.release () Release lock
Use #with lock mode
# no Lock
# number + 1
# number + 3

Import multiprocessing
Import time
Lock = multiprocessing. Lock ()
def add (Number,value,lock):
With Lock:
Print "Init add{0} number={1}". Format (Value,number)
For I in xrange (1,5):
Number + = value
Time.sleep (1)
print "add{0} num = {1}". Format (Value,number)


if __name__ = = "__main__":
Number = 0
Lock = multiprocessing. Lock ()
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"

Operation Result:

Main end
Init add1 number=0
Add1 num = 1
Add1 num = 2
Add1 num = 3
Add1 num = 4
Init add3 number=0
Add3 num = 3
Add3 num = 6
Add3 num = 9
Add3 num = 12

Lock.acquire mode use

Import multiprocessing
Import time
Lock = multiprocessing. Lock ()
def add (Number,value,lock):
# with Lock:
Lock.acquire ()
Try
Print "Init add{0} number={1}". Format (Value,number)
For I in xrange (1,5):
Number + = value
Time.sleep (1)
print "add{0} num = {1}". Format (Value,number)
Except Exception as E:
Raise E
Finally
Lock.release ()

if __name__ = = "__main__":
Number = 0
Lock = multiprocessing. Lock ()
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"
Operation Result:
Main end
Init add1 number=0
Add1 num = 1
Add1 num = 2
Add1 num = 3
Add1 num = 4
Init add3 number=0
Add3 num = 3
Add3 num = 6
Add3 num = 9
Add3 num = 12

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 a
Import multiprocessing
Import time
def change (arr):
For I in range (len):
Arr[i] =-arr[i]
if __name__ = = "__main__":
Print "main Start"
arr = multiprocessing. Array (' I ', Range (10))
Print arr[:]
P3 = multiprocessing. Process (target=change,args= (arr,))
P3.start ()
P3.join ()
Print arr[:]


Import multiprocessing
Import time

Def Add (number,add_value):
Try:
print "Init add{0} number={1}". Format (Add_value, number.value)
for i in XR Ange (1, 5):
Old_number_value = number.value
Number.value + = add_value
print "add{0} num {2} = {0} + {1}". Form At (Add_value,old_number_value, number.value)
Print "# # # # # #add {0} has added #####". Format (add_value)
Time.sleep (1)
print "add{0} num = {1}". Format (Add_value, number.value)
except Exception as E:
Raise e

If __name__ = = "__main__":
print "main start"
Number = multiprocessing. Value (' I ', 0)
Print number
P1 = multiprocessing. Process (target=add,args= (number,1))
P2 = multiprocessing. Process (target=add,args= (number,3))
P1.start ()
P2.start ()
print "main end"
Run Result:
Main start
<synchronized wrapper for C_long (0);
Main end
Init add1 number=0
add1 num 1 = 1 + 0
# # # # # # # # # # # #add1 has added #####
Init add3 number=1
add3 num 4 = 3 + 1
# # #add3 has added #####
add1 num = 4
add1 num 5 = 1 + 4
#### #add1 has added #####
add3 num = 5
add3 num 8 = 3 + 5
# # #add3 has added #####
add1 num = 8
add1 nu M 9 = 1 + 8
# # # # #add1 has added #####
add3 num = 9
Add3 num = 3 + 9
# #add3 has added #####
add1 num =
Add1 num 1 +
# # # # # # # # # # # # # # #add1 has added #####
add3 num =
Add3 num = 3 +
# # # #add3 has Adde D #####
Add1 num =
Add3 num = +

Process finished with exit code 0

(vii) 8-1 multi-process lock and shared memory

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.