Python Forum
Pausing a running process? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Pausing a running process? (/thread-14081.html)



Pausing a running process? - MuntyScruntfundle - Nov-14-2018

Hi folks.

Is there anyway using python I can pause a different python app that's running?

Start app 1.
Start app 2.
App 2 pauses app 1 while it does something.
App 2 closes.
App 1 resumes.

I know I could write this in one app on threads, but that's adding to permanent memory use which I'd like to avoid, plus I'd have to write in a whole communication block in App 1, I'd also like to avoid that.

I've thought of using external files or a database field for triggering the pause, but this gets messy if an app stops unexpectedly.

Any other ideas?

Many thanks.


RE: Pausing a running process? - Gribouillis - Nov-14-2018

For the part App 2 closes -> App 1 resumes, you could perhaps use psutil.wait_procs() in App 1. For the part App 2 pauses App 1 you could perhaps use the signal module, it may depend on your platform. See also psutil.send_signal()


RE: Pausing a running process? - woooee - Nov-14-2018

You have to use something like multiprocessing because you want 2 things, App 1 & App 2, to be running at the same time.
'''
Start app 1.  (The program below)
Start app 2.  (import app_2)
               instead of the ctr & print_numbers(),
               call app_2.function_name() using multiprocessing
App 2 pauses app 1 while it does something.  (in code below)
App 2 closes.  (test for close event instead of time.sleep below)
App 1 resumes.
'''
import multiprocessing
import os
import psutil
import time

def print_numbers():
    ctr = 0
    for x in range(100):
        ctr +=1
        print(ctr)
        time.sleep(0.5)

pid=os.getpid()

mp = multiprocessing.Process(target=print_numbers)
mp.start()
p= psutil.Process(mp.pid)
print('pid =', pid, p)
print("status", mp.is_alive())
time.sleep(5)
print("suspend")
p.suspend()

time.sleep(5)
print("resume it", mp.is_alive())
p.resume()
time.sleep(5)
if mp.is_alive():
     print("terminate", mp.is_alive)
     mp.terminate()
     mp.join()
else:
    print("terminated node")
print("status", mp.is_alive())