vendredi 13 septembre 2019

Python Multiprocessing Process.start() wait for process to be started

I have some testcases where I start a webserver process and then run some URL tests to check if every function runs fine.

The server process start-up time is depending on the system where it is executed. It's a matter of seconds and I work with a time.sleep(5) for now. But honestly I'm not a huge fan of sleep() since it might work for my systems but what if the test runs on a system where server needs 6 secs to start ... (so it's never really safe to go that way..) Tests will fail for no reason at all.

So the question is: is there a nice way to check if the process really started.

I use the python multiprocessing module Example:

from multiprocessing import Process
import testapp.server
import requests
import testapp.config as cfg
import time
p = Process(target=testapp.server.main)
p.start()
time.sleep(5)
testurl=cfg.server_settings["protocol"] + cfg.server_settings["host"] + ":" +str(cfg.server_settings["port"]) + "/test/12"  

r = requests.get(testurl)
p.terminate()
assert int(r.text)==12

So it would be nice to avoid the sleep() and really check when the process started ...

Aucun commentaire:

Enregistrer un commentaire