0

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.

Foxes
  • 1,137
  • 3
  • 10
  • 19
  • Does this answer your question? [Is there any way to kill a Thread?](https://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread) or maybe https://stackoverflow.com/questions/2564137/how-to-terminate-a-thread-when-main-program-ends – JonSG May 26 '21 at 15:31
  • @JonSG That seems to be the exact opposite of what I'm looking for, they're asking how to terminate a thread, I'm looking for how *not* to terminate a thread under any circumstances until it's finished. – Foxes May 26 '21 at 15:37
  • 1
    Sorry, that link will help you get out of your worst case, not that it will prevent the termination. – JonSG May 26 '21 at 15:52
  • @JonSG Ah I see, yes that should get me out of my worst case thank you, are you aware of any ways to prevent the termination entirely until the thread is finished though? – Foxes May 26 '21 at 16:08

0 Answers0