In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "introduction to Python threading entities". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Python threading entity description introduction" bar!
We use multithreading technology a lot when we do software development. For example, if you make a download software like flashget, you need to use it, like realplayer, an online video tool, because you have to download media stream and play it at the same time. In fact, there are many examples.
Threads are "lightweight" relative to processes, and the operating system uses fewer resources to create and manage threads. Threads in the program execute in the same memory space and share many of the same resources. How to create a thread object in python: if you want to create a thread object, it's simple, as long as your class inherits threading.Thread, and then first call the _ _ init__ method of threading.Thread in _ _ init__:
T1 = mythread ('T1') print t1.getName (), t1.isDaemon () t1.setDaemon (True) print t1.getName (), t1.isDaemon () t1.start () print 'main thread exit'
This is just an empty thread. I don't want him to pull an empty cart. He has to do some real work for me. Quite simply, override the run () method of the class to include everything you want to do when the thread executes. In the above code, we have the thread output information to the screen every 1 second after execution, and ending getName () 10 times is a method of the threading.Thread class to get the name of the thread object.
Another method, setName (), is, of course, to set the thread object's name. If you want to create a thread, you must first create a thread object. After a thread object is created, it is in the "born" state. How do you get the thread object to run? Simply call the thread object's start () method:
Is that weird? Isn't it already start? Why not call it a "running" state? Actually, there's a reason. Because our computers generally do not have the real parallel processing ability. What we call multithreading is to divide the time into segments, then let one thread execute at regular intervals, then enter the "sleeping" state, and then wake up another thread in the "sleeping".
So cycle runnable- > sleeping- > runnable... Just because the execution speed of the computer is very fast, and the time interval is so small that we can't feel it and think it's going on at the same time. So a thread is only in a runnable state after start, and it is up to the system to schedule when it runs.
When will that thread Python the thread? Generally speaking, when the execution of the run method of a thread object ends or an exception is thrown during execution, then the thread ends. The system automatically cleans up the "dead" status thread. If a thread T1 needs to wait for another thread T2 to finish execution before it can run, then the T2 join () method can be called on T1.
In this way, T1 will wait for T2 to finish after executing the Python thread statement before it will continue to run. if the main thread of a process is finished and the child thread is still executing, then the process will not exit until all the child threads are finished. how to make other child threads retreat obediently with the boss when the main thread ends?
Then set those who are disobedient as obedient boys and use the thread object's setDaemon () method with a Bool parameter. True's words mean that you have to be obedient, my boss (main thread) yell, you should also follow the withdrawal, can not slow down. If it's False, you don't have to be so obedient. The boss allows you to live up to your orders abroad. It is important to note that the setDaemon () method must be called before the thread object calls the start () method, otherwise it has no effect.
At this point, I believe that everyone on the "Python threading entity description" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.