简体   繁体   中英

How can I terminate a Python script manually without interrupting file output?

I am somewhat new to Python, so I imagine this question has a simple answer. But I cannot seem to find a solution anywhere.

I have a Python script that continually accepts input from a streaming API and saves the data out to a file.

My problem when I need to stop the script to modify the code. If I use ctrl-f2, I sometime catch the script while it is in the process of writing to the output file, and the file ends up corrupted.

Is there a simple way to stop Python manually that allows it to finish executing the current line of code?

You can catch the SIGTERM or SIGINT signal and set a global variable that your script routinely checks to see if it should exit. It may mean you need to break your operations up into smaller chunks so that you can check the exit variable more frequently

import signal

EXIT = False

def handler(signum, frame):
    global EXIT
    EXIT = True

signal.signal(signal.SIGINT, handler)


def long_running_operation():
    for i in range(1000000):
        if EXIT:
            # do cleanup or raise exception so that cleanup
            # can be done higher up.
            return
        # Normal operation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM