Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pausing a running process?
#1
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.
Reply
#2
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()
Reply
#3
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()) 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pausing and returning to function? wallgraffiti 1 2,124 Apr-29-2021, 05:30 PM
Last Post: bowlofred
  Get stdout of a running process yok0 0 2,975 Aug-20-2020, 10:12 AM
Last Post: yok0
  Slide show with mouse click pausing aantono 1 2,154 Jan-28-2020, 04:25 AM
Last Post: Larz60+
  How to sharing object between multiple process from main process using Pipe Subrata 1 3,619 Sep-03-2019, 09:49 PM
Last Post: woooee
  Multiprocessing Module Running An Infinite Child Process Even After Completion hsikora 0 3,836 Dec-19-2018, 08:01 AM
Last Post: hsikora
  running just one process shared among uses Skaperen 3 2,927 Aug-07-2018, 12:12 AM
Last Post: Skaperen
  Pausing a Script malonn 4 3,764 Aug-04-2018, 07:58 PM
Last Post: malonn
  Stopped process is still running pawlaczkaj 9 4,508 Apr-08-2018, 10:22 AM
Last Post: Gribouillis
  Pausing a loop with spacebar, resume again with spacebard bigmit37 10 19,726 May-30-2017, 10:28 AM
Last Post: snippsat
  Pausing and playing a while loop. traceon 1 3,847 Jan-24-2017, 09:11 PM
Last Post: wavic

Forum Jump:

User Panel Messages

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