1

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.

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

You mean like a solution in other answers ?
@Rockybilly -- Yes, that's precisely what I mean :-) -- Although, I would probably suggest that you keep the class functionality and the threading separate entirely. m = MyClass(...); thread = Thread(target=m.run); thread.start() doesn't seem too bad and it simplifies the logic and clarifies the purpose of MyClass.
0

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')

Comments

0

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):
        ...

2 Comments

What I meant was, class opens a continuous subprocess, I needed the subprocess to run inside a thread, sorry for the misconception, and thanks for the information.
@Rockybilly Subprocesses don't run inside threads. They run completely outside of the current process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.