In the post Python threading – an intro I introduced something about python threads, I explained the GIL (Global Interpreter Lock) and how to instantiate workers and timers. The GIL is the topic you must know to take full advantages from python threads. In this post I'm going to explain the following topics: Subclassing Thread Instantiating daemon thread Listing all instatiated threads Signaling threads – Subclassing Thread Since the module "threading" is implemented in an object oriented way, every thread corresponds to an object and you can easily subclass it. The simplest way to subclass is adding a parameter to the initialization and modify the run method to print the passed name:''' subclassing threads ''' import threading class SubThread(threading.Thread): ''' SubClass of Thread ''' def __init__(self, name): threading.Thread.__init__(self) self.name = name def run(self): print(self.name)in this way the subclassed thread will print the name passed as soon as the run method is called:if __name__ == '__main__': for i in range(5): t = SubThread('thread-'+str(i)) t.
I guess you came to this post by searching similar kind of issues in any of the search engine and hope that this resolved your problem. If you find this tips useful, just drop a line below and share the link to others and who knows they might find it useful too.
Stay tuned to my blog, twitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address to anybody as we respect privacy.
Stay tuned to my blog, twitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address to anybody as we respect privacy.
This article is related to
python,Tower of Babel,daemon,thread
python,Tower of Babel,daemon,thread
No comments:
Post a Comment