1

I have a Python app that initiates from a main script, let's say a main.py. main.py (since my app is organized) references and imports other .py files within the same directory, that house other functions. As my app is continuously running, it imports such a function from another script, which is also supposed to run forever until it is explicitly cancelled.

Thing is, how would I cancel that specific script, while leaving its affected variables untouched and the main script/larger app still running?

I do not how I would go about targeting a specific function to stop its execution.

4
  • 1
    if im understanding this correctly, you could have a custom exception class within that script that, when called, will stop that script from running, but wont effect the main loop. ill post an example if this sounds like what you're looking for Commented Feb 14, 2019 at 12:03
  • You can just kill your process (not good but easy). Commented Feb 14, 2019 at 12:04
  • Go through this:- docs.python.org/3/library/asyncio.html Commented Feb 14, 2019 at 12:07
  • While I am reading gautamaggarwal's suggestion, stopping the particular function I want through some custom exception that is called from another function, would be ideal indeed. I would greatly appreciate such an example. Commented Feb 14, 2019 at 12:23

2 Answers 2

1

I use a kill function in my utils to kill any unneeded python process who's name I know. Note the following code was tested/works on Ubuntu Linux and Mac OS machines.

def get_running_pids(process_name):
    pids = []
    p = subprocess.Popen(['ps', '-A'], stdout=subprocess.PIPE)
    out, err = p.communicate()
    for line in out.splitlines():
        if process_name in line.decode('utf-8'):
            pid = int(line.decode('utf-8').split(None, 1)[0])
            pids.append(pid)
    return pids

def kill_process_with_name(process_name):
        pids = get_running_pids(process_name)
        for pid in pids:
            os.kill(pid, signal.SIGKILL)
Sign up to request clarification or add additional context in comments.

2 Comments

For the process_name, is this the name of the function that is continuously running, or something else?
I usually start the script I want to kill in a different process from the controlling script.
1

You Could set up user defined, custom, Exceptions. Extending Pythons builtin Exception object. Further reading here : Pythons User Defined Exceptions

CustomExceptions.py:

class HaltException(Exception):
    pass

-

main.py:

from CustomExceptions import HaltException

class Functions():

    def a(self):
       print("hey")
       self.b()
       return "1"
def b(self):
    print("hello")
    raise HaltException()

def main():

    func_obj = Functions()

    try: 
        func_obj.a()
    except HaltException as e:
        pass
    print("Awesome")

main()

Programs may name their own exceptions by creating a new exception class (see Classes for more about Python classes). Exceptions should typically be derived from the Exception class, either directly or indirectly.

2 Comments

This seems like what I would want to do, being able to cancel the script within standard Python, but the logic of this answer confuses me a bit. How is this working?
hey, check out the docs here on user defined exceptions : docs.python.org/3/tutorial/errors.html#user-defined-exceptions can probably explain it better than i

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.