17

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?

second
  • 28,029
  • 7
  • 75
  • 76
Zhorzh Alexandr
  • 1,469
  • 2
  • 17
  • 28
  • 3
    [Celery](http://celery.readthedocs.org/) is a better option for what you're trying to do. – nathancahill May 02 '15 at 20:17
  • I think Celery is a better option too. Not only you can pass jobs, but there's add-ons like monitoring website, etc. – user1157751 May 11 '15 at 21:38
  • I've had experience with both celery and RQ, and I find RQ much easier and friendly to use, the source is highly readable, and works great. – GG_Python May 12 '15 at 03:49
  • 1
    Nameko seems pretty interesting, but their documentation is not very approachable. At this point it doesn't seem that many (any?) projects are using it, aside from the closed-source stuff onefinestay has built with it. It's also notable that their own docs [suggest using flask *instead of* Nameko](https://nameko.readthedocs.org/en/latest/what_is_nameko.html?highlight=flask#when-shouldn-t-i-use-nameko) if you're building a web app meant for human consumption. There's no mention of using the two in tandem. – dano May 12 '15 at 15:46

4 Answers4

14

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.

Matt
  • 2,153
  • 1
  • 18
  • 29
1

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__)
Adam Michalik
  • 9,678
  • 13
  • 71
  • 102
Vijay
  • 141
  • 7
  • This is very similar to @Matt answer, but is a more minimal example. What I cannot fathom is why you had so many imports not needed from the example and why the nameko rpc_proxy was not put somewhere it could be called with a minimal 3 route example – MrMesees Jun 19 '20 at 08:05
0

Have you heard of Pinball? It's Pinterest's own workflow manager.

It allows you to schedule and manage jobs scaling accross multiple machines and it's in Python.

They tout the following design principles:

  • Simple: based on easy to grasp abstractions
  • Extensible: component-based approach
  • Transparent: state stored in a readable format
  • Reliable: stateless computing components
  • Scalable: scales horizontally
  • Admin-friendly: can be upgraded without aborting workflows
  • Feature-rich: auto-retries, per-job-emails, runtime alternations, priorities, overrun policies, etc.
Guilherme Rodrigues
  • 2,818
  • 1
  • 17
  • 22
0

The question is why you want to combine flask and nameko? Both of them are API building frameworks (both of them have http request handling endpoints).

In our project we have started with flask. For some short period of time when we went into microservices, we had flask and nameko together in one microservice, but there was more problems than gains from this combination.

My tip: you should treat flask and nameko as an API provider. If you need nameko to do microservices I suggest to dump flask. The main question is what features flask give you that nameko cannot provide? Maybe you can achieve it by first-party code or other (easier to combine with than flask) libraries.

jorzel
  • 1,216
  • 1
  • 8
  • 12