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

The common properties of Python threads and the process of directly inheriting the subclass threading.Thread

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

Share

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

This article introduces the knowledge of "the common properties of Python threads and the process of directly inheriting the subclass threading.Thread". In the actual case operation, 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!

1. Common thread attributes

1.threading.currentThread: returns the current thread variable

2.threading.enumerate: returns a list containing the running thread, which refers to the state after the thread starts and before the end

3.threading.activeCount: returns the number of running threads with the same effect as len (threading.enumer)

4.thr.setName: set a name for a thread

5.thr.getName: get the name of the thread.

For example:

Mport _ thread as threadimport timedef loop1 (in1): print ("Start loop1 at:", time.ctime ()) print ("I am the parameter", in1) time.sleep (4) print ("End loop1 at:", time.ctime ()) def loop2 (in1, in2): print ("Start loop2 at:", time.ctime ()) print ("I am the parameter", in1, "and parameter", in2) time.sleep (4) print ("End loop2 at:" Time.ctime () import threadingdef main1 (): print ("Starting at:", time.ctime ()) T1 = threading.Thread (target = loop1, args = ('',) t1.setName ("THR_1") # rename the thread t1.start () T2 = threading.Thread (target = loop2, args = (',') t2.setName ("THR_2") t2.setDaemon (True) # the main thread is finished when it is finished No need to wait for thread 2t2.start () time.sleep (3) # two child threads are still running after three seconds Because one of them paused for thr in threading.enumerate () for four seconds: # returned a list of running subthreads print ("running subthreads name: {0}" .format (thr.getName () # read the thread's name print ("number of running subthreads is: {0}" .format (threading.activeCount () # printed out the number of threads There are three threads, t1.join () #, including the main thread and two child threads, and thread 1 runs down print ("ALL done at:", time.ctime ()) if _ _ name__ = "_ _ main__": main1 ()

Second, directly inherit the subclass threading.Thread

1. Directly inherit the Thread; rewrite run function

two。 Example:

Class MyThread (threading.Thread): # defines a subclass def _ init__ (self, args) of Thread: # overrides the _ _ init__ function, where the argument is self and the newly introduced parameter super (MyThread, self). _ _ init__ () # fixed format Inherit the _ _ init__ function of the parent class self.args = argsdef run (self): time.sleep (1) print ("The args for this class is {0}" .format (self.args)) for i in range (5): t = MyThread (I) t.start () t.join ()

That's all for "the common properties of Python threads and the process of directly inheriting the subclass threading.Thread." 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.

Share To

Development

Wechat

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

12
Report