1

I am trying to setup two eventlet servers that listen concurrently on different ports.

The first server in the code below is for a SocketIO implementation and the second is for an external connection. Both function separately but not at the same time.

if __name__ == '__main__':
    eventlet.wsgi.server(eventlet.listen(('0.0.0.0', 4000)), app)
    s = eventlet.listen(('0.0.0.0', 6000))
    pool = eventlet.GreenPool(5)
    while True:
        c, address = s.accept()
        pool.spawn_n(function, c)
mdod
  • 13
  • 3

1 Answers1

3

The problem is that the eventlet.wsgi.server() function does not return, it runs the loop that listens and handles HTTP requests for your Flask-SocketIO server.

What you need to do is move one of the two servers to a background thread. For example, you can move the Flask-SocketIO server to a background thread as follows:

if __name__ == '__main__':
    eventlet.spawn(eventlet.wsgi.server, eventlet.listen(('0.0.0.0', 4000)), app)
    s = eventlet.listen(('0.0.0.0', 6000))
    pool = eventlet.GreenPool(5)
    while True:
        c, address = s.accept()
        pool.spawn_n(function, c)

If you prefer to move your other server, you can do something like this:

def other_server():
    s = eventlet.listen(('0.0.0.0', 6000))
    pool = eventlet.GreenPool(5)
    while True:
        c, address = s.accept()
        pool.spawn_n(function, c)

if __name__ == '__main__':
    eventlet.spawn(other_server)
    eventlet.wsgi.server(eventlet.listen(('0.0.0.0', 4000)), app)
Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
  • Thanks! I don't know why I tried to ```spawn``` the function call when I tried this before. – mdod Jul 02 '19 at 05:04