Pausing a process?

前端 未结 4 1170
轻奢々
轻奢々 2020-12-17 17:29

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? Possib

相关标签:
4条回答
  • 2020-12-17 17:56

    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.

    0 讨论(0)
  • 2020-12-17 18:11

    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()

    0 讨论(0)
  • 2020-12-17 18:13

    By using psutil ( https://github.com/giampaolo/psutil ):

    >>> import psutil
    >>> somepid = 1023
    >>> p = psutil.Process(somepid)
    >>> p.suspend()
    >>> p.resume()
    
    0 讨论(0)
  • 2020-12-17 18:16

    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.

    0 讨论(0)
提交回复
热议问题