static-GTFS-manager
static-GTFS-manager copied to clipboard
Graceful Ctrl+C to terminate program
In Ubuntu, on pressing Ctrl+C in the terminal, here's the terminal output:
ERROR:tornado.application:Uncaught exception
Traceback (most recent call last):
File "site-packages/tornado/web.py", line 1541, in _execute
File "GTFSManager.py", line 591, in get
stats = GTFSstats(dbfile)
File "<string>", line 126, in GTFSstats
File "site-packages/tinydb/database.py", line 172, in table
File "site-packages/tinydb/database.py", line 269, in __init__
File "site-packages/tinydb/database.py", line 359, in _read
File "site-packages/tinydb/database.py", line 81, in read
File "site-packages/tinydb/storages.py", line 98, in read
File "/tmp/VIRTUAL/lib/python3.5/codecs.py", line 213, in setstate
def setstate(self, state):
KeyboardInterrupt
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "site-packages/tornado/http1connection.py", line 237, in _read_message
File "site-packages/tornado/routing.py", line 256, in finish
File "site-packages/tornado/web.py", line 2139, in finish
File "site-packages/tornado/web.py", line 2172, in execute
File "site-packages/tornado/gen.py", line 315, in wrapper
File "site-packages/tornado/web.py", line 1541, in _execute
KeyboardInterrupt
pastCommits GET call took 0.01 seconds.
^CTraceback (most recent call last):
File "GTFSManager.py", line 1262, in <module>
[8753] Failed to execute script GTFSManager
In Windows, pressing Ctrl+C initially has no effect : it is "put in queue". Then, on the browser if we navigate to another page or click something that triggers an API call, THEN the program finds that Ctrl+C had been pressed and it terminates. Again with the error lines if it was running on a command prompt.
There's an added issue here that if the browser tab had already been closed and the user came to the dos-box, they'll keep pressing Ctrl+C and nothing will happen, because the program is waiting for an API call and the keypresses will not give any response.
So, solution needed that will listen for a Ctrl+C keypress and exit the program gracefully, maybe even with a thank-you message if possible.
This might help: https://stackoverflow.com/a/1112350/4355695
Adapting from https://nattster.wordpress.com/2013/06/05/catch-kill-signal-in-python/ :
import signal, sys
def signal_term_handler(signal, frame):
print('\nClosing Program.\nThank you for using GTFS Manager. Website: https://github.com/WRI-Cities/static-GTFS-manager/\n')
sys.exit(0)
signal.signal(signal.SIGINT, signal_term_handler)
Works in linux. Have to test in Windows and then in the windows standalone executable.
Reporting from Windows!
Had to put the signal.signal.. command inside the main function to make it work when running in python3 on windows. Then it exits gracefully. So this works when we're running the python scripts.
BUT, the windows binary is still showing the same effect as previously: There is no response on pressing Ctrl+C, and you have to change a page or otherwise make an API call on the browser side to make the program acknowledge the Ctrl+C and exit. So the fix works at python script side, in both linux and windows, but not on standalone executable side yet.
import signal, sys
def signal_term_handler(signal, frame):
print('\nClosing Program.\nThank you for using GTFS Manager. Website: https://github.com/WRI-Cities/static-GTFS-manager/\n')
sys.exit(0)
# have to put line inside the main program:
if __name__ == "__main__":
signal.signal(signal.SIGINT, signal_term_handler)
# rest of program