Run fastapi gunicorn outside main thread

Viewed 351

So I have two microapp inside my app, there are:

  1. fastapi for webservice
  2. kafka for consuming message

I want to run both of them in a different thread within the main thread, I have run kafka on a separate thread, but I don't know how to run gunicorn fastapi, I have read that we can use subprocess in thread.

Is there a way to run gunicorn programmatically like uvicorn.run()?

Here is my main code:

def create_app():

    logging.basicConfig(level=logging.DEBUG)

    # register config
    config = Config()

    # register dependency
    di_container = Container()
    di_container.config.from_dict(config.get_config())
    di_container.wire(modules=[runner_rating])

    # start kafka consumer in a separate thread
    kafka_client = di_container.kafka_consumer.client()
    kafka_client.listen()
   
    # how to run this inside thread?
    app = FastAPI()
    app.container = di_container

    return app
0 Answers
Related