Skip to content Skip to sidebar Skip to footer

How Can I Stop The Execution Of A Python Function From Outside Of It?

So I have this library that I use and within one of my functions I call a function from that library, which happens to take a really long time. Now, at the same time I have another

Solution 1:

You cannot terminate a thread. Either the library supports cancellation by design, where it internally would have to check for a condition every once in a while to abort if requested, or you have to wait for it to finish.

What you can do is call the library in a subprocess rather than a thread, since processes can be terminated through signals. Python's multiprocessing module provides a threading-like API for spawning forks and handling IPC, including synchronization.

Or spawn a separate subprocess via subprocess.Popen if forking is too heavy on your resources (e.g. memory footprint through copying of the parent process).

I can't think of any other way, unfortunately.

Solution 2:

Generally, I think you want to run your long_running_function in a separate thread, and have it occasionally report its information to the main thread.

This post gives a similar example within a wxpython program.

Presuming you are doing this outside of wxpython, you should be able to replace the wx.CallAfter and wx.Publisher with threading.Thread and PubSub.

It would look something like this:

import threading
import time

def myfunction():
    # subscribe to the long_running_function
    while True:
        # subscribe to the long_running_function and get the published data
        if condition_met:
            # publish a stop command
            break
        time.sleep(1)

def long_running_function():
    for loop in loops:
        # subscribe to main thread and check for stop command, if so, break
        # do an iteration
        # publish some data

threading.Thread(group=None, target=long_running_function, args=())  # launches your long_running_function but doesn't block flow
myfunction()

I haven't used pubsub a ton so I can't quickly whip up the code but it should get you there.

As an alternative, do you know the stop criteria before you launch the long_running_function? If so, you can just pass it as an argument and check whether it is met internally.

Post a Comment for "How Can I Stop The Execution Of A Python Function From Outside Of It?"