Rockybilly
Rockybilly

Reputation: 4520

Custom class inheritance from threading.Thread

I am writing a class that has many functionalities(therefore methods), but I require this class to run inside a thread(class opens a subprocess). I want to use the common way of declaring thread based classes of,

class HiStackOverflow(threading.Thread):
     # Somethings...

However, as I said, this class of mine has many pseudo-private, regular and static methods. And as I declare them, I want to avoid overriding some necessary threading.Thread method by mistake.

Well I can always check the directory of threading.Thread and see if there are any method names that overlap, however this seemed like a inappropriate way to handle this. It may be impractical as the method count increases.

My question is, is this kind of implementation feasible ? If not, how should I handle this ? Should I write some wrapper class as the Thread handler.

Thanks in advance.

Upvotes: 1

Views: 2917

Answers (3)

mgilson
mgilson

Reputation: 310097

If you're worried about namespace clashes between your class and threading.Thread, I would definitely suggest that you use composition rather than inheritance (or keep the two functionalities separate entirely). There shouldn't be significant overhead to just wrapping the couple threading methods that you need and then name clashes become a non-issue.

It also more cleanly will separate the functionality of your class from the functionality provided by threading. That's likely to be a win in the long run for understanding your code.

Upvotes: 1

viraptor
viraptor

Reputation: 34205

There seem to be some ideas conflated in this phrase:

but I require this class to run inside a thread(class opens a subprocess)

  1. Classes don't "run". You can start a new thread which executes some class method, or an instance method. That class doesn't have to inherit from Thread. It doesn't even need a reference to the running thread. You just start to execute some function in a new thread and you're done.

  2. Subprocesses are unrelated to threads. You don't need one to do the other.

If you're worried about overriding something, check the documentation (https://docs.python.org/3/library/threading.html#thread-objects). Otherwise, if you want to keep the reference to the thread, you can always do:

class HiStackoverflow:
    def run(self):
        self.thread = Thread(target=self.entry_point)
        self.thread.start()

    def entry_point(self):
        ...

Upvotes: 0

tdelaney
tdelaney

Reputation: 77387

There isn't much benefit from inheriting from Thread. You could have a factory method that creates the thread or even have its __init__ do it.

import threading
import time

class MyClass:

    def __init__(self):
        self._thread = threading.Thread(target=self.run)
        self._thread.start()

    def run(self):
        for i in range(5):
            print('worker thread', i)
            time.sleep(.5)

    def join(self):
        self._thread.join()

my_obj = MyClass()
for i in range(3):
    print('main thread', i)
    time.sleep(.5)
my_obj.join()
print('done')

Upvotes: 0

Related Questions