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 > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to understand Python processes and threads". In the operation of actual cases, many people will encounter such a dilemma, so 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!
Understanding of processes and threads
The thread is the smallest unit that the operating system can schedule the budget, and it is the actual operating unit in the process.
The Linux process has a parent process and a child process, and the process in the window system is equal
A standard thread consists of thread ID, current instruction pointer, register set, and stack. In many systems, creating a thread is 1-100 times faster than creating a process.
Each process thinks it has exclusive access to all calculator hardware resources
Processes are independent kingdoms, and data cannot be easily shared between processes.
Threads are provinces, and threads within the same process can share the resources of the process, and each process has its own independent stack.
Processes and threads in Python
The process starts an interpreter process, and threads share an interpreter process.
Thread
Create the simplest threaded program
Import threading
Def worker ():
Print ("hello world")
T = threading.Thread (target=worker) # create thread object
T.start () # start
Printing using the print function (because the thread switches back to interrupting the execution of the print function) can lead to thread unsafety. Logging is recommended.
Import logging
Import threading
FORMAT ='% (asctime-15s\ t [% (threadName) s,% (thread) 8d]% (message) s)'
Logging.basicConfig (level=logging.INFO, format=FORMAT)
Def worker ():
For i in range (100):
Logging.info ("{} is running" .format (threading.current_thread () .name))
For i in range (5):
Name = "workr- {}" .format (I)
Threading.Thread (target=worker) .start ()
Thread safety: if a thread executes a piece of code that does not produce uncertain results, then the code is thread-safe
Daemon thread and no-daemon thread
The daemon here is not a daemon in linux
If there is a non-daemon thread, the main thread will not kill all the non-daemon threads when it exits, until all the daemon threads are finished. If there are still threads, the main thread needs to exit, and it will end all the daemon threads exiting.
Threading.local
Python provides the threading.local class, which is instantiated to get a global object, but the data stored by different threads using this object is not visible to other threads.
Ctx = threading.local ()
Ctx.x = 123
The Threading.local class builds a large dictionary whose element is a mapping of the address of each thread instance as key and the thread object reference thread as a separate dictionary.
{id (Thread)-> (ref (Thread), thread-local dict)}
Through the threading.local instance, we can safely use the unique data of different threads, so that the data between threads can be isolated, and it is as safe as local variables.
Threading.Timer
Threading.Timer inherits from Thread, a class that defines how often a function is executed
Class threading.Timer (interval, function, args=None, kwargs=None)
After the Start method is executed, the Timer object is in a waiting state, and after waiting for interval, it starts to execute the
If the cancle method is used during the wait before executing the function, the end of executing the function is skipped.
Def worker ():
Print ("hello world")
T = threading.Timer (5dint worker)
This is the end of the content of "how Python processes and threads understand". Thank you for 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.