-
-
Save zedr/7b72f9a8be97df80f17fb8f97cd50019 to your computer and use it in GitHub Desktop.
Fibonacci microservice using Python & Tornado.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tornado.web | |
import tornado.ioloop | |
from tornado.options import define, options | |
define('port', default=45000, help='try running on a given port', type=int) | |
def fib(): | |
a, b = 1, 1 | |
while True: | |
yield a | |
a, b = b, a+b | |
class IndexHandler(tornado.web.RequestHandler): | |
def get(self): | |
self.write('Fibonacci microservice.') | |
class NextFibHandler(tornado.web.RequestHandler): | |
fib_iter = fib() | |
def get(self): | |
response = str(next(self.fib_iter)) | |
self.write(response) | |
class First1000Handler(tornado.web.RequestHandler): | |
def get(self): | |
res = [] | |
fib_iter = fib() | |
for i in range(0, 1000): | |
res.append(next(fib_iter)) | |
self.write(str(res)) | |
def make_app(): | |
return tornado.web.Application([ | |
(r'/', IndexHandler), | |
(r'/next', NextFibHandler), | |
(r'/onethousand', First1000Handler) | |
]) | |
if __name__ == '__main__': | |
tornado.options.parse_command_line() | |
app = make_app() | |
app.listen(options.port) | |
print('==================================') | |
print('Starting Fibanacci Microservice...') | |
print('Listening on port: ' + str(options.port)) | |
print('\r\n') | |
tornado.ioloop.IOLoop.current().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment