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.