In todays world of microservices, Flask is a great compact framework in Python for quickly creating web services. For simple services, it's faster to work with then Django (which incidentally is great for more complex services/sites).
For instance, you can build a self-contained service that serves up random price data over JSON, in 15 lines of code!
You start by installing Flask:
pip install Flask
Then create a simple wrapper around your library. In this instance our library is one we have seen before which generates prices using a geometric Brownian motion process.
from flask import app, Flask, jsonify, request import bm app = Flask(__name__) @app.route('/gbm') def gbm(): args = request.args periods = int(args.get('periods')) start_price = float(args.get('startPrice')) mu = float(args.get('mu')) sigma = float(args.get('sigma')) delta = float(args.get('delta')) prices = bm.generate_gbm_prices(periods, start_price, mu, sigma, delta) return jsonify(result=prices.tolist()) if __name__ == "__main__": app.run()
We the run our application:
$ python price_service.py * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) 127.0.0.1 - - [23/Jun/2015 21:33:06] "GET /gbm?periods=100&startPrice=70.0&mu=0.05&sigma=0.30&delta=1.0 HTTP/1.1" 200 - ...
And verify we're getting data back!
You can use the following unit test to validate the service:
import json import unittest from services import price_service class ServicesTest(unittest.TestCase): def setUp(self): self.app = price_service.app.test_client() def test_gbm(self): data = { 'periods': 100, 'startPrice': 70.0, 'mu': 0.05, 'sigma': 0.30, 'delta': 1.0 } response = self.app.get('/gbm', query_string=data) self.assertEqual(200, response.status_code) data = json.loads(response.get_data().decode('utf-8')) self.assertEqual(100, len(data['result']))
This gives you a nice easy way to scale up your dumb (non-persistent) services to reside onto separate hosts or containers.
Full code is available here.
Categories: Python
Hi, nice post. I am just found out about Flask (and totally new to it). It seems like a great way to parallelize apps. One question I have is where the producer publishing the data? Additionally, would appending it in to a global queue work so other processes (say the strategy) can read off of it?
Thanks,
M
In this example the producer is the price generator in our bm module. A web-based service such as this example works in a request/response manner, making it not directly suitable for publication to a queue. To achieve your goal, you have a few options.
1. Create a component that continually polls the service and publishes the data to a queue (pretty inefficient).
2. Write a queue publisher that repeatedly generates new data via the bm module publishing this data directly to a queue (I'd recommend this approach).
3. Use websockets or HTTP2's server push technology. See here for more info http://stackoverflow.com/questions/12232304/how-to-implement-server-push-in-flask-framework.
This is great. If I rewrite my first stab at a web service (volkills.org) I think I'll just use something like this. Django has too much boiler plate...
I initially thought the tags below referred to some sort of odd markdown somehow, not html...
volkills.org
Yeah, Django is great for building web-sites, but not lightweight services.
There's also Tornado if you want async support built in.