During the process of code optimization, I encountered a problem in which I defined several global variables in a process, and then I process a few sub-processes, can I modify the global variables individually? Which value does the last global variable take?
After trying to get the result:
The child process inherits the global variables of the parent process and is done as a copy, so the modified global variables of the child process only have an impact on itself and on its own child processes.
Parent-child processes do not share these global variables, which means that changes to global variables in the parent process do not affect the global variables in the child process, nor do the child processes affect the parent process.
In order to realize the communication of the parent-child process, the value and array methods were found after the online search.
The value function returns a shared memory wrapper class that contains a cTYPES object
General integers with I, characters with C, floating-point numbers with D on it can be
The array function returns a shared memory wrapper class that contains an array in which the string needs to be passed by array
- @args the values contained in the shared memory
- @lock The default value is true: Create a new lock to control access to value. This parameter can also be multiprocessing. Lock or multiprocessing. Rlock, used to control access to value
=============================================================================================================== =================
Case one:
defworker (num, mystr, arr): Num.value*= 2Mystr.value="OK" forIinchRange (len (arr)): Arr[i]= Arr[i] * (-1) + 1.5defdump_vars (num, mystr, arr):Print 'Num:', Num.valuePrint 'Str:', mystr[:]Print 'arr:', arr[:]if __name__=='__main__': Num= Value ('I', 5) MyStr= Array ('C','Just for Test') Arr= Array ('D', [1.0, 1.5,-2.0]) dir (str)Print 'init value'dump_vars (num, mystr, arr) PS= [Process (Target=worker, args= (num, mystr, arr)) forXinchRange (3)] forPinchPs:p.start () forPinchPs:p.join ()Print Print 'After all workers finished'dump_vars (num, mystr, arr)
# Results: init valuenum: 5str: for testarr: [1.0, 1.5, -2.0]after All workers Finishednum: +str: OK
Multiple tests I found that when a string is shared, the initialization in the main process determines the length of the string,
The length of the string is fixed after creation, which is equivalent to copying the address of the string to a pointer and recording its length at the first address of the string.
Reading this value later will read the fixed-length content, regardless of the current length of the new content, for example:
For example we initialize a string "ABCDE" in the main process, once initialized, the length is fixed, now the length is 5, and then we assign values in other processes, we try to assign a value of
"ABCDEFG", at this time the execution will be error, because the length exceeded, we are assigned to "Yes", the final output is "YES&EFG" (Here & is a non-display character, different environments display different, it is possible to display a space, may show null). That is, the length is not good, must and initialization string and so on.
This concludes that if you want to share a string, you must assign a string of equal length to the child process. It is recommended that the length of the string be checked first in the child process and then the string of the specified length is stitched as needed
Case TWO:
fromMultiprocessingImportProcess, Value, Arraydeff (N, a): N.value= N.value + 1 forIinchRange (len (a)): A[i]= a[i] * 10if __name__=='__main__': Num= Value ('I', 1) Arr= Array ('I', Range (10)) P= Process (Target=f, args=(num, arr)) P.start () P.join ()Print(Num.value)Print(arr[:]) P2= Process (Target=f, args=(num, arr)) P2.start () P2.join ()Print(Num.value)Print(arr[:])#The output is:#2#[0, ten , +, +, +,--#3#[0, Max, Max, +, +, +
Python multi-process Value, array application record