Is there a way to pause a process (running from an executable) so that it stops the cpu load while it's paused, and waits till it's unpaused to go on with its work? Possibly in python, or in some way accessible by python.
-
do you mean thread or running system process ? – ukanth May 10 '10 at 16:11
-
1I would be inclined to lower process priority rather than "pausing" it. That way other processes will go first – Nas Banov Nov 20 '10 at 00:25
4 Answers
By using psutil ( https://github.com/giampaolo/psutil ):
>>> import psutil
>>> somepid = 1023
>>> p = psutil.Process(somepid)
>>> p.suspend()
>>> p.resume()

- 12,488
- 6
- 68
- 60
you are thinking of SIGTSTP -- the same signal that happens when you push CTRL-Z
. This suspends the process until it gets SIGCONT.
of course, some programs can just catch and ignore this signal, so it depends on the executable. however, if you can suspend and resume it manually, you can do it from a python program, too. use os.kill()

- 3,307
- 3
- 29
- 41
-
1and how do I unsuspend it so that it continues work from where it was before? – kettlepot May 10 '10 at 16:28
-
7SIGSTOP can't be blocked and would be slightly more accurate in this context, not coming from a tty. http://en.wikipedia.org/wiki/SIGSTOP – msw May 10 '10 at 19:23
I just implemented this with signals in python something like this:
def mysignalhandler(sig, frame):
print "Got " + str(sig)
if sig == signal.SIGUSR1:
do_something()
signal.signal(signal.SIGUSR1, mysignalhandler)
signal.pause()
This will pause at the last line and call do_something()
when it receives the signal USR1, for example through a
kill -USR1 <pid>
command.
This will only work in UNIX though.

- 2,905
- 2
- 33
- 36
There is a (almost) native way of doing this in Python, and it's quite simple :
import time
time.sleep(5)
In this snippet, 5
is the number of seconds you want to pause your program.
-
8This doesn’t answer the question; which is about pausing *another* process. – bfontaine Jul 25 '17 at 16:30