I have a program that will scrape data from a user's personal music charts and then input that data into a Google Spreadsheet, and at certain points in this program, such as during the initial setup of the sheet, it must not be stopped for any reason, otherwise it will break when trying to actually input the data into the sheet. I thought I had fixed this by using threads, with the code:
from threading import Thread
a = Thread(target=sheets_setup)
a.start()
a.join()
Which would work perfectly in the Python IDLE, and would prevent the code from exiting until setup was complete, even with a KeyboardInterrupt. However, when compiling the program to an .exe, or running it in cmd, the program will stop execution of the code in the case of a KeyboardInterrupt. However, it still won't fully close the program, instead hanging indefinitely with this shown in the console:
Traceback (most recent call last):
File "main.py", line 455, in <module>
a.join()
File "C:\Python39\lib\threading.py", line 1029, in join
self._wait_for_tstate_lock()
File "C:\Python39\lib\threading.py", line 1045, in _wait_for_tstate_lock
elif lock.acquire(block, timeout):
KeyboardInterrupt
I assume that this is caused because it's running in a thread, but it keeping the program alive without actually continuing the code execution is probably the worst case scenario here. Is it possible to get it to continue code execution until the thread is finished, even in the case of a KeyboardInterrupt?
EDIT: I figured out the problem, it had nothing to do with the threads, but rather the fact that I'm running a Selenium Webdriver and doing Ctrl+C in the console was causing the webdriver to close, even though the code would continue, causing errors as the code would require a webdriver that no longer exists.