I have 2 applications elephant
and mouse
both are WSGI apps made with Flask. How can I run both applications on a single port but on different processes? My use case is that I want multiple instances of mouse
but only a single instance of elephant
but I don't want the hassle of host:port
config for their comms. I tried running these apps separately using gunicorn and specifying --workers
and it works but then the apps run on different ports.
like so ..
elephant_cmd = ['gunicorn', '-b', '0.0.0.0:5009', '-w', '10', 'elephant:app', '-n', 'worker', '--access-logfile', '-']
mouse_cmd = ['gunicorn', '-b', '0.0.0.0:5001', '-w', '2', 'mouse:app', '-n', 'model', '--access-logfile', '-']
elephant = subprocess.Popen(worker_cmd)
mouse = subprocess.Popen(model_cmd)
while not kill_signal:
time.sleep(1)
if elephant.poll() is None or mouse.poll() is None: continue
else:
worker.send_signal(15)
model.send_signal(15)
Is there a more elegant way to do it? What if I have multiple apps? I also explored gunicorn invocation through app and using multiprocessing
to call these applications.
I am expecting that there can be an interface like so..
from path.to.elephant import app as elephant
from path.to.mouse import app as mouse
app = SomeMiddleware(apps=[elephant, mouse], num_workers=[1, 10])
And I can run a cmd like gunicorn path.to.root:app -w 1