Python Threading - Creation of a subclass?

2019-08-08 21:24发布

问题:

I am having a problem wrapping my brain around the reason for creating a subclass when using threading in python. I've read a number of websites including tutorialspoint.

The docs say you need to define a new subclass of the Thread class. I have a basic understanding of classes but haven't played with subclasses at all. I haven't had to do anything like this yet with any other modules I've used like os & ftplib. Can anyone point me to a site that may explain this better for a newbie scripter?

#!/usr/bin/python

import threading

class myThread (threading.Thread):

I was able to write my own script without creating this subclass and it works so I am not sure why this is stated as a requirement. This is my simple little script I created to help me understand threading initially.

#!/usr/bin/python

# Import the necessary modules
import threading
import ftplib

# FTP function - Connects and performs directory listing
class
def ftpconnect(target):
        ftp = ftplib.FTP(target)
        ftp.login()
        print "File list from: %s" % target
        files = ftp.dir()
        print files

# Main function - Iterates through a lists of FTP sites creating theads
def main():
    sites = ["ftp.openbsd.org","ftp.ucsb.edu","ubuntu.osuosl.org"]
    for i in sites:
        myThread = threading.Thread(target=ftpconnect(i))
        myThread.start()
        print "The thread's ID is : " + str(myThread.ident)

if (__name__ == "__main__"):
    main()

Thanks for the help!

I am using tutorialspoint.com for my reference material. It sounds like your saying I am biting off more than I can chew and I should keep it simple at this point considering I don't need to use the more complicated options yet. This is what the site says:

Creating Thread using Threading Module:
To implement a new thread using the threading module, you have to do the following:

- Define a new subclass of the Thread class.

- Override the __init__(self [,args]) method to add additional arguments.

- Then, override the run(self [,args]) method to implement what the thread should do when started.

Once you have created the new Thread subclass, you can create an instance of it and then start a new thread by invoking the start(), which will in turn call run() method.

回答1:

The docs say you need to define a new subclass of the Thread class.

and

I was able to write my own script without creating this subclass and it works so I am not sure why this is stated as a requirement.

The Python docs say no such thing, and can't guess which docs you're talking about. Here are Python docs:

There are two ways to specify the activity: by passing a callable object to the constructor, or by overriding the run() method in a subclass. No other methods (except for the constructor) should be overridden in a subclass. In other words, only override the init() and run() methods of this class.

You're using the first method specified there (passing a callable to the Thread() constructor). That's fine. Subclasses become more valuable when the callable needs access to state variables and you don't want to clutter your program with globals for that purpose, and especially when using multiple threads that each need their own state variables. Then state variables can usually be implemented as instance variables on your own subclass of threading.Thread. If you don't need that (yet), don't worry about it (yet).