Get the App
SLTechnology News&Howtos  ›  Development  › 

Methods that Python custom classes inherit from threading.Thread

Shulou Source: shulou.com Published: 2022-05-31 17:49:17 10月17日 Update

This article introduces the relevant knowledge of "the method of inheriting threading.Thread from Python custom class". 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!

Description

1. Multitasking program development can be completed by using threading module.

2. In order to make the encapsulation of each thread more perfect, when using the threading module, a new subclass class is usually defined, which only needs to inherit threading.Thread, and then rewrite the run method.

Example

"" use of Python multithreading "" import timeimport threading class MyThread (threading.Thread): # def _ _ init__ (self): # super (). _ init__ () def run (self): for i in range (3): time.sleep (1) msg = "Isimm" + self.name+' @'+ str (I) # name attribute What is saved is the name of the current thread print (msg) def main (): t = MyThread () t.start () if _ _ name__ = ='_ _ main__':main () "the method that the Python custom class inherits threading.Thread" ends here. Thank you for your 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!

Tags: Methods threads content more modules knowledge utility learning next tasks just names dilemmas subclasses instances actual properties frequent meetings situations articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Xiaomi Docker MariaDB Microsoft