0

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

  • first link on Google: [running multiple sites with gunicorn-nginx](https://stackoverflow.com/questions/3756934/running-multiple-sites-with-djangos-sites-framework-through-gunicorn-nginx). Instead of `django`, for you would be `flask`. Another [good resource](https://realpython.com/django-nginx-gunicorn/) - you'd have to choose your web server as: `nginx` or `apache http` or something else. – azbarcea Nov 30 '22 at 17:22
  • helpful stack:- https://stackoverflow.com/questions/1694144/can-two-applications-listen-to-the-same-port – Ajay K Dec 01 '22 at 06:27

0 Answers0