'Killing a thread executing a function

I need to call a function to do some actions. Also, I need to stop it abruptly: I am not looking for a gentle and clean stop because that's the purpose of my test.

Let's assume the call I want to make is the following one:

class_1.function_1(arg_1=True)

First of all, I cannot use multiprocessing.Process (I tried), because for some reason this does not allows me to call properly other function of class_1.

So, here is what I did:

thread = threading.Thread(target=class_1.function_1, args=(True,))

Is runs like a charm, but I have one problem: how can I force stop thread? For information, I cannot edit function_1 nor class_1.

Here is some solution I tried, without success:

  • calling _stop()
  • del thread

If you have any ideas, I am interested, even without threads.

Thanks!



Solution 1:[1]

you can use python-worker link

from worker import worker

@worker
def my_function():
    ...

my_worker = my_function()

## put some code

my_worker.abort()

my_function will automatically run as a thread and my_worker.abort() will stop it immediately

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 danangjoyoo