In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Python standard library is a lot of preliminary process, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, hope you can gain something.
We know to use the subprocess package to create child processes, but this package has two major limitations:
1) We always have subprocess run external programs instead of functions written inside a Python script.
2) text communication between processes is carried out only through pipes. The above limits the application of subprocess packages to a wider range of multiprocess tasks. (this comparison is actually unfair because subprocessing itself is designed to be a shell, not a multi-process management package.)
Threading and multiprocessing
The multiprocessing package is a multi-process management package in Python. Similar to threading.Thread, it can use the multiprocessing.Process object to create a process. The process can run functions written within the Python program. This Process object is used in the same way as the Thread object, and there are also methods for start (), run (), join (). In addition, there are Lock/Event/Semaphore/Condition classes in the multiprocessing package (these objects can be passed to individual processes through parameters like multithreading) to synchronize processes, using the same as the classes of the same name in the threading package. As a result, a large part of multiprocessing uses the same set of API as threading, but in a multi-process context.
However, when using these shared API, we should pay attention to the following points:
On the UNIX platform, when a process terminates, the process needs to be called wait by its parent process, otherwise the process becomes a Zombie. Therefore, it is necessary to call the join () method (effectively equivalent to wait) on each Process object. For multithreading, this is not necessary because there is only one process.
Multiprocessing provides IPC (such as Pipe and Queue) that are not available in the threading package, which is more efficient. Pipe and Queue should be given priority, and synchronization methods such as Lock/Event/Semaphore/Condition should be avoided (because they do not occupy the resources of the user process).
Multiple processes should avoid sharing resources. In multithreading, we can easily share resources, such as using global variables or passing parameters. In the case of multiple processes, the above approach is not appropriate because each process has its own independent memory space. At this point, we can share resources by sharing memory and Manager. However, this increases the complexity of the program and reduces the efficiency of the program because of the need for synchronization.
PID is stored in Process.PID, and PID is None if the process does not already have start ().
We can see the similarities and differences between Thread objects and Process objects in usage and results in the following program. Each thread and process does one thing: print the PID. The problem is that all tasks are printed to the same standard output (stdout). In this way, the output characters will be mixed and cannot be read. Using Lock synchronization, after one task output is completed, another task output is allowed, which can avoid multiple tasks being output to the terminal at the same time.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.