Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i combine flask and nameko?

How can i combine Flask web application and Nameko microservices?

Let me give you some context. I have flask-based (http://flask.pocoo.org) web application. This application can execute long (5-10 minutes) tasks. I want to be able to write and attach additional modules to this application while it is still running. It is OK if I stop application while in development, but I cannot stop it in production.

I dont have any experience with Nameko (https://nameko.readthedocs.org), is it the best solution to my problem? And if so - can I mix Flask app and Nameko microservices?

like image 946
Zhorzh Alexandr Avatar asked May 02 '15 12:05

Zhorzh Alexandr


2 Answers

Contributor to nameko here. I agree with nathancahill that celery is a good choice for this.

You absolutely can use nameko and Flask together. There’s a short example in a gist here: https://gist.github.com/mattbennett/4250ce5d56b36a99bc39

In that configuration though, you're covering the same ground that Celery was built for -- namely handling long-running tasks outside the request-response cycle. Frankly the example in the gist would be much better implemented exclusively as a nameko app (using the built-in http entrypoint), because it’s not using any of the more advanced web framework-like features that Flask gives you.

If you want to write microservices, even ones that are predominantly HTTP based, nameko provides some nice tooling for doing so. If you just want to add async processing to an existing webapp, celery would be the standard choice.

like image 179
Matt Avatar answered Nov 17 '22 08:11

Matt


In gateway import flask

from flask import Flask
app = Flask(__name__)

Example:

import json
from nameko.rpc import RpcProxy
from nameko.web.handlers import http
from flask import Flask, render_template, request, redirect, url_for

app = Flask(__name__)
like image 23
Vijay Avatar answered Nov 17 '22 07:11

Vijay