![]() |
How to prevent Command Promt from closing when I turn it off in Python? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to prevent Command Promt from closing when I turn it off in Python? (/thread-24829.html) |
How to prevent Command Promt from closing when I turn it off in Python? - binhduonggttn - Mar-06-2020 I am very new to python. I have a little program and I want it run in the following order : - First I set a method time.sleep(10)within code to suspends execution for the 10 seconds in my test file. - I run my test file by command prompt. - While the program is performing a suspension of 10 seconds, suddenly I click the X close button in the top-right corner of the window, the program will not immediately shut down but it will run command print('Close terminal event detected.')and delay about 1 second by time.sleep(1)before it shuts off. I wrote the code below but I'm still stuck. Would someone please tell me how to do it? Thank you. import time import os time.sleep(10) # If the terminal is closed while time.sleep(10) is running, the program will print the following line and run time.sleep(1) before exiting print('Close terminal event detected.') time.sleep(1) RE: How to prevent Command Promt from closing when I turn it off in Python? - ibreeden - Mar-06-2020 I think you need this: signal — Set handlers for asynchronous events. If you are on Unix or Linux I think you had best catch signal number 1. Symbolic name: SIGHUP. The "hangup signal". The hangup signal is the oldest signal and it stems from the time one had to connect to the computer with a terminal via a telephone modem. When you hung up the phone the connection was lost. To prevent the processes from continuing to run, Unix sent a SIGHUP signal to all the processes connected to that terminal, causing them to stop. I think closing a terminal window will also send a SIGHUP to your running process. |