In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about the difference between start and run methods in python. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.
Conclusion: when starting a thread, if the target is assigned and the run method is not overridden, the thread start will directly call the corresponding method in target.
The specific code is as follows:
1. Initialize a thread
Threading.Thread.__init__ (self,target=thread_run ()) def _ _ init__ (self, group=None, target=None, name=None, args= (), kwargs=None, *, daemon=None): assert group is None "group argument must be None for now" if kwargs is None: kwargs = {} self._target = target self._name = str (name or _ newname ()) self._args = args self._kwargs = kwargs
2. Call start to start the thread
Finally, the _ start_new_thread method is called, and self._bootstrap is passed as a parameter.
Thread1.start () def start (self): if not self._initialized: raise RuntimeError ("thread.__init__ () not called") if self._started.is_set (): raise RuntimeError ("threads can only be started once") with _ active_limbo_lock: _ limbo [self] = self try: _ start_new_thread (self._bootstrap) () except Exception: with _ active_limbo_lock: del _ limbo [self] raise self._started.wait ()
3. _ start_new_thread is equivalent to starting a new thread and calling the callback function in the new thread
_ start_new_thread = _ thread.start_new_threaddef start_new_thread (function: Callable [..., Any], args: tuple [Any,...], kwargs: dict [str, Any] =...)-> int:.
4. The callback function executed is the self._bootstrap passed above. The _ bootstrap method calls _ bootstrap_inner () directly, while bootstrap_inner calls the run method.
Def _ bootstrap_inner (self): try: self._set_ident () self._set_tstate_lock () if _ HAVE_THREAD_NATIVE_ID: self._set_native_id () self._started.set () with _ active_limbo_lock: _ Action [self. _ ident] = self del _ limbo [self] If _ trace_hook: _ sys.settrace (_ trace_hook) if _ profile_hook: _ sys.setprofile (_ profile_hook) try: self.run ()
5. Finally call the run method
Def run (self): try: if self._target: self._target (* self._args, * self._kwargs) finally: # Avoid a refcycle if the thread is running a function with # an argument that has a member that points to the thread. Del self._target, self._args, self._kwargs
Conclusion:
If the run method is overridden, the overridden run method is called directly
If the run method is not overridden and the target is defined, the target method at thread creation time is called directly, otherwise nothing is done
There is a problem here:
Specify the target parameter, and during execution, the printed process name mainthread (main process), instead of the previously assigned process name
Threading.Thread.init (self,target=thread_run ())
After analysis, it is found that target is given the body of the executed function, so the thread_run function will be executed first. After execution, the return value of thread_run is assigned to target, because thread_run has no return value, so the value of target is None. If the run function is not overridden at this time, the thread will do nothing. The execution of thread_run is in the main thread, not in the child thread, as we think.
Def thread_run (): print ("overwrite: start thread:" + threading.current_thread () .name) time.sleep (2) print ("overwrite: exit thread:" + threading.current_thread () .name) class myThread (threading.Thread): def _ init__ (self, threadID, name, delay): threading.Thread.__init__ (self Target=thread_run () self.threadID = threadID self.name = name self.delay = delay thread1.start () thread1.join () print ("exit main thread")
Running result:
Overwrite: start thread: MainThread
Overwrite: exit thread: MainThread
Exit the main thread
That's all of the article "what's the difference between start and run in python?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.