Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does python manage processes like threads

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how python manages processes like threads". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how python manages processes like threads.

First, create a process

The easiest way to create a process is to instantiate a Process object with an objective function and then call the start () function like threading to make it work. Examples are as follows:

Import multiprocessingdef worker (): for i in range (3): print (I) if _ _ name__== "_ _ main__": P = multiprocessing.Process (target=worker) p.start ()

It is important to note that the multiprocessing library creation process on Windows must be in if _ _ name__== "_ _ main__":, which is a multi-process implementation problem on Windows. On Windows, the child process automatically import starts its file, and executes these statements when import. If it is created directly, the child process will report an error recursively infinitely. Therefore, the part of creating the child process must be protected with that if judgment. When import, _ _ name__ is not _ _ main__, it will not run recursively.

Second, set the process name

In the threading thread, we can set the thread name through its parameter name, and we can also set the name of its process through the name parameter. Examples are as follows:

Import multiprocessingimport timedef worker (): print (multiprocessing.current_process (). Name, "start") time.sleep (2) print (multiprocessing.current_process (). Name, "end") if _ _ name__ = = "_ main__": p1 = multiprocessing.Process (name='p1', target=worker) p2 = multiprocessing.Process (name='p2', target=worker) p3 = multiprocessing.Process (name='p3') Target=worker) p1.start () p2.start () p3.start () III. Daemon

Like threads, the main program does not exit until all child processes have exited. Sometimes we may need to start a background process that can run all the time without blocking the exit of the main program.

To flag a daemon, you can add the third parameter, daemon, to True. The default value is False, not as a daemon. Examples are as follows:

Import multiprocessingimport timedef worker (): print (multiprocessing.current_process (). Name, "start") time.sleep (1) print (multiprocessing.current_process (). Name, "end") def worker2 (): print (multiprocessing.current_process (). Name, "start") time.sleep (2) print (multiprocessing.current_process (). Name, "end") if _ name__ = "_ main__": P1 = multiprocessing.Process (name='p1') Target=worker) p2 = multiprocessing.Process (name='p2', target=worker2, daemon=True) p3 = multiprocessing.Process (name='p3', target=worker2, daemon=True) p1.start () p2.start () p3.start ()

P2PowerP3 is a daemon, but p1 is not, so after 1 second of execution, it exits the main program and does not print the contents of the p2p3. But it is still in execution until the execution is complete.

4. Join ()

Similarly, if you expect to force the end of a daemon, you can add the join () function. It is the same as the above code. The example is as follows:

Import multiprocessingimport timedef worker (): print (multiprocessing.current_process (). Name, "start") time.sleep (1) print (multiprocessing.current_process (). Name, "end") def worker2 (): print (multiprocessing.current_process (). Name, "start") time.sleep (2) print (multiprocessing.current_process (). Name, "end") if _ name__ = "_ main__": P1 = multiprocessing.Process (name='p1') Target=worker) p2 = multiprocessing.Process (name='p2', target=worker2, daemon=True) p3 = multiprocessing.Process (name='p3', target=worker2, daemon=True) p1.start () p2.start () p3.start () p1.join () p2.join () p3.join ()

After running, the result is the same as setting the process name, which is not shown here. The only difference from the daemon code is the last three lines of the join () function code. Of course, like a thread, you can pass in a time to the join () function, after which the main process no longer waits.

V. forced termination of the process

If a process has been suspended or accidentally entered a deadlock, then we tend to end the process forcefully. Calling terminate () on a process object terminates the child process. Examples are as follows:

Import multiprocessingimport timedef worker (): print (multiprocessing.current_process (). Name, "start") time.sleep (5) print (multiprocessing.current_process (). Name, "end") if _ _ name__ = = "_ _ main__": p1 = multiprocessing.Process (name='p1', target=worker) p1.start () print ("still running", p1.is_alive ()) p1.terminate () print ("still running" P1.is_alive () p1.join () print ("still running", p1.is_alive ())

After terminating the process, use the join () function to wait for the exit of the process. Give the process management code enough time to update the state of the object to reflect that the process has been terminated.

VI. Process exit status code

When the process exits, the generated status code can be accessed through the exitcode property. The following table shows the value range of its status code and its meaning:

Exit code meaning 0 does not generate any error > 0 process has an error and exits with that error code

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report