Note that there are some explanatory texts on larger screens.

plurals
  1. POPython Threading - Creation of a subclass?
    primarykey
    data
    text
    <p>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 <a href="http://www.tutorialspoint.com/python/python_multithreading.htm" rel="nofollow">tutorialspoint</a>.</p> <p>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 &amp; ftplib. Can anyone point me to a site that may explain this better for a newbie scripter?</p> <pre><code>#!/usr/bin/python import threading class myThread (threading.Thread): </code></pre> <p>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.</p> <pre><code>#!/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() </code></pre> <p>Thanks for the help!</p> <p>I am using <a href="http://www.tutorialspoint.com/python/python_multithreading.htm" rel="nofollow">tutorialspoint.com</a> 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:</p> <pre><code>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. </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload