Python Forum
Is it Possible to pause a script?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is it Possible to pause a script?
#1
Greetings!
I was wondering if it is possible to pause a Python script when it is running.
Ctrl+C aborts a script,
Is there anything I can do to pause it?

Thank you.
Reply
#2
(Mar-31-2023, 09:58 PM)tester_V Wrote: Is there anything I can do to pause it?
Yes, there is a way. In Unix/Linux there is a rich number of signals you can send to a process. I am not sure what is implemented of it in Windows.
I understand you run your program from the terminal/dosbox, for you say you do a <Ctrl>C. This sends the signal SIGINT (Interrupt) to the process. You can catch those signals. Read all about it in "signal — Set handlers for asynchronous events".
Example:
#! /usr/bin/env python3
import signal
import time

def handler(signum, frame):
    input("Progam paused. Hit enter to resume")
    return

signal.signal(signal.SIGINT, handler)

for i in range(10):
    print(i)
    time.sleep(1)
Output:
$ ./signalexample.py 0 1 2 ^CProgam paused. Hit enter to resume 3 4 5 6 ^CProgam paused. Hit enter to resume 7 8 9
Gribouillis and tester_V like this post
Reply
#3
Outstanding!
ibreeden, thank you very much!
Reply
#4
Pause a process: CTRL + Z
Continue the process: fg
Gribouillis likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
(Apr-03-2023, 01:21 PM)DeaD_EyE Wrote: Pause a process: CTRL + Z
Continue the process: fg

No words! You are Da Man Wink and I'm probably confused but CTRL+Z does not work...
What does it mean "Continue the process: fg" ?

Thank you
Reply
#6
(Apr-05-2023, 05:50 AM)tester_V Wrote: I'm probably confused but CTRL+Z does not work...
It works if you start the program from the linux terminal and then type ^Z in the terminal.

If you want to do the same outside the terminal, you could perhaps send SIGTSTP to the process to pause it and then SIGCONT to resume it.
tester_V likes this post
Reply
#7
I see.
Thank you!
You guys are awesome!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I pause only one object? actualpy 1 368 Feb-01-2024, 07:43 PM
Last Post: deanhystad
  How to pause execution (on windows)? pstein 1 567 Jun-23-2023, 06:58 PM
Last Post: rob101
  Did subprocess.Popen() causes main routine to pause stdout? liudr 4 3,665 May-04-2021, 08:58 PM
Last Post: liudr
  Not able to make a specific thing pause in pygame cooImanreebro 4 3,230 Dec-13-2020, 10:34 PM
Last Post: cooImanreebro
  How to Make Python code execution pause and resume to create .csv and read values. Kashi 2 3,777 Jun-14-2018, 04:16 PM
Last Post: DeaD_EyE
  how to pause pyxhook keylogger johweb 0 2,379 Jun-13-2018, 03:17 PM
Last Post: johweb
  How to pause/interupt with keyboard jehoshua 5 7,234 Mar-20-2018, 05:52 AM
Last Post: jehoshua
  Pause event Trajme 2 3,107 Dec-06-2017, 11:01 PM
Last Post: hshivaraj
  how to enter a pause in the middle of a file sylas 2 3,293 Jun-11-2017, 06:46 AM
Last Post: sylas

Forum Jump:

User Panel Messages

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