Saturday 7 November 2020

Kill Process, containing an infinite loop, clearly on multiple os

I have a Python3 program, which shall be tested with linux and windows with Python 3.6+.

For thos testenviroment is a TestServer needed which shall run in the Background as Process (from multiprocessing), while the tests are running. The tests are running with unittests.

The TestServer process(called mainloop) looks like this:

def mainloop(somearg):
    server = mylib.server.Server()
    # some initialisation stuff
    server.start()
    while True: # How to break/handle this if process shall be killed?
        while True:  # How to break/handle this if process shall be killed?
            event = server.get_event()
            if event:
                logger.info(event)
            else:
                break
        time.sleep(1)
    server.stop() # never been reached
    server.cleanup()  # never been reached

My setUpClass looks like this:

from multiprocessing import Process
from mylib import mainloop

# some other stuff

@classmethod
def setUpClass(cls):
    cls.process = Process(target=mainloop)
    cls.process.start()
    time.sleep(2) # wait for server to start

@classmethod
def tearDownClass(cls):
    kill(cls.process.pid,1)

Questions:

  1. How can I handle that if "mainloop" shall be killed, it is able to stop and is cleaned up a proper way?
  2. How can I manage that for windows and linux systems?
  3. What is best practice for such testing purposes (handle multiple os and test on each multiple python versions)?


from Kill Process, containing an infinite loop, clearly on multiple os

No comments:

Post a Comment