1

What I'm trying to do is a GUI program. I want a button that will pause the script where it is, and when I hit the button again, It will resume.

I've seen this question and this question.

Problem is, they are both dealing with the console, and the recomendations for those appear to be looking for keypresses, which isn't what I want to achieve.

Is there some way to just kind of freeze the script where it is, and then start it back up like nothing ever happened?

Community
  • 1
  • 1
evamvid
  • 831
  • 6
  • 18
  • 40
  • "pause the script where it is", is this in some sort of loop? – Drewness Mar 15 '14 at 20:27
  • WeaselFox's answer below is what you want, unless you are trying to walk the program (or something more intimate), in which case you should probably hook into the python debugger (`pydb`). – U2EF1 Mar 15 '14 at 20:43

2 Answers2

3

you could freeze the process - (like ctrl+z in linux) and restart it whenever :

>>> import psutil
>>> somepid = 999
>>> p = psutil.Process(somepid)
>>> p.suspend()
>>> p.resume()
WeaselFox
  • 7,220
  • 8
  • 44
  • 75
0

OK, you are not giving much information, but I adapted a similar answer that I gave in other post, because I think it could give you the solution. the example is with Threads, because is the first thing that came to my mind to do what you want. One Thread (func_control) takes care of the input and the other (func_1) does the real job.

The idea is that when you press Enter the function doing the job will pause or resume. It is important to have the sleep(0.1) in order to not overload the CPU. I have not included anything to finish the execution, but I think you can do that alone... ;-) Enjoy!

from threading import Thread

def func_control():
    global switch
    switch = 1
    while True:
        if switch:
            print "I'm doing stuff. Press Enter to pause me!"
        else:
            print "I'm doing nothing. Press Enter to resume!"

        if sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
            line = raw_input()
            switch = 0 if switch == 1 else 1

def func_1():
    while True:
        if switch != 0:
            # Put all you need to do here!
            pass
        else:
            sleep(0.1)
    return 0

switch = 1

myThread1 = Thread(target=func_1)
myThread1.daemon = True

myThread1.start()

func_control()
Javier
  • 1,027
  • 14
  • 23