0

I have a Process, which does not repeat (Selenium Webdriver), which can run up to 100 minutes and more. My problem is that I cant figure out a way to pause in the middle of the process.

I've been trying to resolve it with Events, but so far I just found solutions, which would stop the process after an iteration, I've been thinking that it would theoretically be possible to put a query after every line of code, which asks if event is set to continue processing, but that will be very hard, because I have about thousand lines of code in that function.

In this example I have also been trying to pause the main Process over a Event which is called from another process, which starts when the user gives an Input(e.g. Enter), but the processes are not connected and therefor the evet_func is still running further.

import time
import multiprocessing

def event_func(num):
    print('\t%r has started' % multiprocessing.current_process())
    for i in range(1000):
        time.sleep(0.1)
        print(i)
    print('\t%r has finished' % multiprocessing.current_process())

def event_wait(event):
    print("Event Wait called")
    event.wait()
    print("Event wait finished")

if __name__ == "__main__":
    event = multiprocessing.Event()

    m = multiprocessing.Process(target=event_func, args=(1,), daemon=True)
    m.start()

    x = input("Waiting for input: ")

    p = multiprocessing.Process(target=event_wait, args=(event,))
    p.start()

    print ('main is sleeping')
    time.sleep(2)

    print ('main is setting event')
    event.set()

    m.join()

My expected result is that the process is pausable in the middle of the function and not after an iteration.

Sem_jong
  • 41
  • 12

2 Answers2

0

Using an object of threading.Event you can achieve the following resume and pause functionality.

Seems duplicate of :: Thread that I can pause and resume?

shyamzzp
  • 115
  • 7
0

You can use os.kill to send signal.SIGSTOP and signal.SIGCONT to your process to pause it and continue it:

import time
import multiprocessing
import os
import signal

def event_func(num):
    print('\t%r has started' % multiprocessing.current_process())
    for i in range(1000):
        time.sleep(0.1)
        print(i)
    print('\t%r has finished' % multiprocessing.current_process())

if __name__ == "__main__":
    event = multiprocessing.Event()
    m = multiprocessing.Process(target=event_func, args=(1,), daemon=True)
    m.start()
    x = input("Waiting for input: ")
    print("Pausing process...")
    os.kill(m.pid, signal.SIGSTOP)
    time.sleep(5)
    print("Continuing process")
    os.kill(m.pid, signal.SIGCONT)
    m.join()
  • If you have to support Windows, you may want to look at [psutil](https://pypi.org/project/psutil/). However, it appears to rely on undocumented functions. –  Aug 01 '19 at 06:58