Python Forum
How to prevent Command Promt from closing when I turn it off in Python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to prevent Command Promt from closing when I turn it off in Python?
#1
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)
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Using SQLAlchemy, prevent SQLite3 table update by multiple program instances Calab 3 756 Aug-09-2023, 05:51 PM
Last Post: Calab
Smile How we can prevent screen recording murad_ali 3 1,839 Jul-29-2022, 10:29 AM
Last Post: DeaD_EyE
  python-docx regex : Browse the found words in turn from top to bottom Tmagpy 0 1,535 Jun-27-2022, 08:45 AM
Last Post: Tmagpy
  How to prevent python from going to new line in for loop? idknuttin 3 4,945 Feb-11-2022, 05:40 AM
Last Post: deanhystad
  my python file closing rwahdan 1 1,441 Jun-30-2021, 08:25 AM
Last Post: Gribouillis
  Reading data to python: turn into list or dataframe hhchenfx 2 5,392 Jun-01-2021, 10:28 AM
Last Post: Larz60+
  Prevent urllib.request from using my local proxy spacedog 0 2,887 Apr-24-2021, 08:55 PM
Last Post: spacedog
  "Automate the Boring Stuff with Python" creating a path works but only for CMD promt Milos 2 2,879 Nov-28-2020, 01:08 PM
Last Post: Larz60+
  Turn coordinates in string into a tuple Kolterdyx 3 2,776 Jun-10-2020, 05:04 AM
Last Post: buran
  Prevent a PDF from being edited using Python? PythonNPC 1 2,298 May-05-2020, 09:04 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020