In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the method of sharing data between python processes". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the method of sharing data between python processes".
Def worker (num, mystr, arr): num.value * = 2 mystr.value = "ok" for i in range (len (arr)): arr [I] = arr [I] * (- 1) + 1.5def dump_vars (num, mystr, arr): print 'num:', num.value print 'str:', mystr [:] print 'arr:' Arr [:] if _ name__=='__main__': num = Value ('just for test', 5) mystr = Array (' censor, 'just for test') arr = Array (' dink, [1.0,1.5,2.0]) dir (str) print 'init value' dump_vars (num, mystr, arr) ps = [Process (target=worker, args= (num, mystr) Arr)) for x in range (3)] for p in ps: p.start () for p in ps: p.join () print print 'after all workers finished' dump_vars (num, mystr, arr)
The above code prints the result:
Pay attention to the content in the red box, and I have tested many times and found that when sharing a string, initialization in the main process determines the length of the string.
After creation, the length of the string is fixed, which is equivalent to copying the address of the string to a pointer and recording its own length at the first address of the string.
When you read this value later, you will read that fixed length of content, regardless of the current length of the new content, for example:
For example, we initialize a string "abcde" in the main process, and once initialized, the length is fixed, now the length is 5, and then we assign a value in other processes, and we try to assign a value to
"abcdefg", at this time, the execution will report an error, because the length exceeds the limit, we assign it to "yes", and the final output is "yes&efg" (here & represents a character that cannot be displayed, which is displayed differently in different environments, may show spaces, and may show null). In other words, no matter how long or short, it must be as long as the initialization string.
So it comes to the conclusion that if you want to share a string, you must assign a string of the same length when assigning a value in a child process. It is recommended that you check the length of the string in the child process, and then concatenate the string of specified length as needed.
The above analysis is based on the following, please take a look at the following basic usage:
Sharing data among multiple processes, using multiprocessing.Value and multiprocessing.Array
Value (typecode_or_type, * args [, lock])
The Value function returns a shared memory wrapper class that contains a ctypes object
The list of typecode_or_type typecode is as follows
-
Type code C Type Python Type
'c' char character
'b' signed char int
'B' unsigned char int
'u 'Py_UNICODE Unicode character
'h' signed short int
'H'unsigned short int
'i' signed int int
'I 'unsigned int long
'l 'signed long int
'l 'unsigned long long
'f 'float float
'd'double float
Array (typecode_or_type, size_or_initializer, *, lock=True)
The default value for lock is True: create a new lock to control access to value. This parameter can also be a multiprocessing.Lock or multiprocessing.RLock object to control access to value
Thank you for your reading, the above is the content of "the method of sharing data between python processes". After the study of this article, I believe you have a deeper understanding of the method of sharing data between python processes, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.