In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "Python multi-process knowledge collation". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1.Python multiprocess
Type CPU intensive operation IO intensive operation network request intensive operation linear operation 94.9182499622.461999957.3296 multithread operation 101.170000124.86050010.505333265 multiprocess operation 53.88999912.784000040.504500032
From the above table, we can see:
Multithreading does not seem to have a great advantage under IO-intensive operations (perhaps the heavy tasks of IO operations can show the advantage), and the performance of multithreading is obviously worse than that of single-thread linear execution under CPU-intensive operations, but for busy thread blocking operations such as network requests, the advantage of multithreading is very significant.
Multiple processes can show performance advantages in both CPU-intensive and IO-intensive, as well as network request-intensive (operations that often cause thread blocking). However, in similar network request-intensive operations, it is almost the same as multithreading, but it takes more resources such as CPU, so in this case, we can choose multithreading to execute.
Constructor description method description target execution method name process name args execution method instance method description is_alive () check whether the process is running join () blocking process start () start process run () is called by start () stop process property description name process name pid process number daemon daemon
two。 Examples
2.1 use multithreading to run functions
Import multiprocessing as mpdef count (): for i in range (1000): print (I) # run the next code only when the program is the main program # each python module (python file, that is, test.py and import_test.py here) contains the built-in # variable _ _ name__, when the running module is executed, _ _ name__ equals the file name (including the suffix .py) If # import is in another module, _ _ name__ equals the module name (excluding the suffix .py). And "_ _ main__" equals the name of the file executed before # (including the suffix .py). Then when the module is executed directly, _ _ name__ = = 'main' knot # is true. If _ _ name__ = = "_ _ main__": # Note that the function called by target here is the function name, not with (); in addition, the first letter of Process is capitalized p1=mp.Process (target=count) p2=mp.Process (target=count) p1.start () p2.start ()
We can see a fragment like this in the run result:
one hundred and fifty two
nine hundred and fifty three
one hundred and fifty three
one hundred and fifty four
one hundred and fifty five
one hundred and fifty six
one hundred and fifty seven
nine hundred and fifty four
one hundred and fifty eight
nine hundred and fifty five
one hundred and fifty nine
nine hundred and fifty six
one hundred and sixty
Indicates that in the process of executing the first loop, the second process starts to execute concurrently, and the two loop processes are carried out at the same time.
2.2 daemon
Import multiprocessing as mpdef count1 (): for i in range (10): print (I) def count2 (): for i in range (10): print (iTun10) if _ _ name__ = "_ _ main__": p1=mp.Process (target=count1) p2=mp.Process (target=count2) # the properties of the process must be set p1.daemon=Truep1.start () p2.start () print ("Done") before the process starts
The result of the output is:
Done
0
ten
twenty
thirty
forty
fifty
sixty
seventy
eighty
ninety
We can see that the main program ends first and prints "Done". Because the process of count1 is not protected, the count1 process is terminated after the end of the main program. However, due to the degree of protection set by count2, the count2 process does not terminate with the termination of the main program.
2.3 lock
Lock is used to handle access to shared resources and avoid access conflicts
2.4 Semaphore
Semaphore restricts the number of visits
2.5 Event
Event is used to realize synchronous communication between processes.
2.6 Queue
Multi-process security queue, which is used to realize data transfer between processes.
2.7 Pipe
2.8 Pool
This is the end of the content of "Python Multi-process knowledge points arrangement". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.