Sunday, 27 December 2020

handling grpc server with flask app in one thread _ Flask grpc

I have a flask app that runs on port 4000, and I wrote a gprc server which is runs on port 4010.

the problem is when I import grpc server function into my flask app, after the flask app comes up grpc server closes, and there is no error. I've been searched and noticed after running the grpc server you need to sleep the program in some way. so, I found this code:

import threading
stop_event = threading.Event()
stop_event.wait()

this code is preventing the grpc server to stop, however, my problem is the flask app is not running correctly. I assume that it is because of the sleeping or stoping threading. event(). I think I have to run my grpc in another process or thread. but I don't know how? is there any configuration I missed or any technical issue about the flask structure that conflicts with grpc?

edit:

I tried to use multithreading package .with this configuration before running the flask app:

p = multiprocessing.Process(target=GRPC())
p.start()
p.join()
socketio.run(app, port=os.environ.get('PORT'), host="0.0.0.0")

I was thinking that running grpc server in another process could be helpful. grpc server starts correctly, but when we said the grpc await, the flask app didn't run. it's weird; because, running gprc server in another process, shouldn't affect my main process.



from handling grpc server with flask app in one thread _ Flask grpc

No comments:

Post a Comment