The Task
Imagine we're making a webservice called "Primes". It has 1 endpoint, something like this:
from flask import Flask
import random
app = Flask('primes')
@app.route("/rand_prime")
def rand_prime(n):
return random.sample(primes, 1)
if __name__ == "__main__":
global primes
# some function to load primes from a file into a set
primes = load_primes()
app.run()
We want the fastest webservice to serve this endpoint that handles as much QPS as possible.
Constraints
- We have to load all the primes into memory, there's no database at all.
- We cannot use memcache, redis, etc.
- We have to use Python.
Current Solution
The code above just serves the primes via the default webserver that comes bundled with Flask. Some ideas to improve upon this:
- Use a better webserver (eg. Tornado)
- Proxy several instances of this behind an Nginx server
Can we make this even faster? We really don't need complicated routing or security... Is it possible to do better by writing a custom webserver?
Aucun commentaire:
Enregistrer un commentaire