0

In my main python file that controls the program, I have a function that creates a thread for a function that has been passed in as a parameter.

def NewThread(com, Returning: bool, thread_ID, *arguments) -> Any:
    """
    Will create a new thread for a function/command.
    :param com: Command to be Executed
    :param Returning: True/False Will the command return anything?
    :param thread_ID: Name of thread created
    :param arguments: Arguments to be sent to Command that is started in a thread
    """
    
    class NewThreadWorker(Thread):
        def __init__(self, group = None, target = None, name = None, args = (), kwargs = None, *,
                     daemon = None):
            Thread.__init__(self, group, target, name, args, kwargs, daemon = daemon)
            self.daemon = True
            self._return = None
        
        def run(self):
            if self._target is not None:
                self._return = self._target(*self._args, **self._kwargs)
        
        def join(self):
            Thread.join(self)
            return self._return
    
    ntw = NewThreadWorker(target = com, name = thread_ID, args = (*arguments,))
    if Returning:
        ntw.start()
        return ntw.join()
    else:
        ntw.start()

This function works and is great, however I have no way of interacting with the new thread that has been created. Usuallly, all you would have to do stop the thread is (in this case) ntw.join() however I have no way of doing this with my current setup. So after many trials and errors (many, many errors) I thought: Why can't I just assign the thread to a new variable and use .join() on that?

So, that now brings me to my question:

how can you kill a thread in python3 when you do not have access to the variable that started the thread (ntw in this case)?

also if it is possible I would like keep the setup of NewThread function. (I don't really want to have manage each thread manually for every function I have, that would be very painful) Any help is greatly appreciated.

If you want to check out where the source code is for the project that this is integrated with, here is the github repo: https://github.com/vipersniper0501/CP_Scripts2/blob/dev/GUIs/ScriptRunnerPyQt5_GUI/ScriptGUIrunner.py

EDIT: I have looked Is there any way to kill a Thread? and this does not answer my question.

ViperSniper0501
  • 300
  • 6
  • 15
  • Does this answer your question? [Is there any way to kill a Thread?](https://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread) – metatoaster Dec 21 '20 at 13:08
  • Kind of but not really. I only have access to the thread when it is first started in the NewThread function, and for most of threads I use this function are classes that do not have any `return` in them. It would be best if I was able to control the thread from within itself, as only in thread will it no when it is time to kill itself. – ViperSniper0501 Dec 21 '20 at 13:12
  • 1
    I could elaborate an answer- and might still do - but you have to be aware you are reinventing the wheel there. Please, look at the documentation for concurrent.futures: the stdlib in Python has a way to create a pool of worker threads that can perform a task in another thread, return it result, and even reuse a thread, with a minimal of fuss: https://docs.python.org/3/library/concurrent.futures.html – jsbueno Dec 21 '20 at 13:23
  • Here is the problem. After thoroughly looking through the examples provided in the Is there any way to kill a Thread? link, all of the examples given are assuming that I am trying to kill the thread from the main thread where it was called. I am trying to kill the thread from within itself that is in a completely different file and in a different directory. The examples provided also mostly used timers as in: after this amount of time kill the thread. This is not possible for me. However, for all I know it could totally be possible and that I am just not able to find a way to implement it. – ViperSniper0501 Dec 21 '20 at 14:30
  • If it's code inside a thread that wants to terminate itself, just raise some kind of exception, which will then terminate the thread. – metatoaster Dec 22 '20 at 00:29

0 Answers0