I am writing a web service based on Klein framework
https://klein.readthedocs.io/en/latest/index.html
At this stage I am stress testing my service, it can handles about 70 requests per second on amazon t2.medium instance. But when I use top to check the server, it only use 100% of CPU. I think amazon t2.medium instance should have 2 cpu, so I wonder is there a way to change in my web service code to use all of the possible cpus and hopefully handle more requests.
I've read python documentations and found the multiprocessing module but I am not sure will that be the right solution to it. Right now the main function of my web service is
APP = Klein()
if __name__ == "__main__":
APP.run("0.0.0.0", SERVER_PORT)
Is there a straight forward fix to make this service being able to use multiple cpu to process the incoming requests? Thank you for reading the question.
It's certainly possible to use
multiprocessingand it sure as heck is easy to spin up processes.In an enterprise environment it's better and more reliable to run behind a dedicated load balancer like nginx. So the snippet above should only be used to start the web servers, then all your load balancing should be handled by a dedicated load balancer.
Keep the multiprocess code to a bare minimum or else basic things like debugging and shared system files start to become an annoyance. And that's the "normal" stuff, there are TONS of ABNORMALITIES that can arise and no one will be able to help you because you don't know what's happening yourself. Just running this snippet, I noticed a few weird things with signals and Twisted. I think it could be fixed if I ran all
kleinimports in therunserver().Get informed, learn from other's mistakes, heed the warnings of those who have been burned by
multiprocessingand go make a kick ass app! Hope this helps :DReferences