Maximum number of client connections in Flask-SocketIO with Eventlet

| Comments

It’s not mentioned in the docs for Flask-SocketIO that Eventlet has an option max_size which by default limits the maximum number of client connections opened at any time to 1024. There is no way to pass it through flask run command, so the application should be run with socketio.run, for example:

...
if __name__ == '__main__':
    socketio.run(app, host='0.0.0.0', port='8080', max_size=int(os.environ.get('EVENTLET_MAX_SIZE', 1024)))

How to split Celery tasks file

| Comments

Suppose there is a large tasks.py file, like this:

@celery.task()
def task1():
    ...

@celery.task()
def task2():
    ...
    task1.delay()
    ...

...

A good idea is to split it on the smaller files, but Celery auto_discover by default search tasks in package.tasks module, so one way to do this is to create a package tasks and import tasks from other files in __init__.py.

__init__.py

from .task1 import task1
from .task2 import task2

__all__ = ['task1', 'task2']

task1.py

@celery.task()
def task1():
    ...

task2.py

from .task1 import task1

@celery.task()
def task2():
    ...
    task1.delay()
    ...

CSRF exempt for Flask-RESTPlus API

| Comments

The @csrf.exempt method does not work with Resource methods or decorators, it should be done on Api level. Here is an example how to exclude resources from CSRF protection based on class:

def csrf_exempt_my_resource(view):
    if issubclass(view.view_class, MyResource):
        return csrf.exempt(view)
    return view

api_blueprint = Blueprint('api', __name__)
api = Api(api_blueprint, title='My API', decorators=[csrf_exempt_my_resource])

Or for all resources:

api_blueprint = Blueprint('api', __name__)
api = Api(api_blueprint, title='My Private API', decorators=[csrf.exempt])

Function as model column in Flask Admin

| Comments

I like the idea about separation of business logic from models and views into services. There are more details in these slides from the EuroPython talk.

Usually in Flask-Admin for a new column a new method with @property decorator is added and the model becomes fat. Also it’s not good to put something with queries in property. Another way is to put this function to the services and use column formatter in Flask-Admin.

def get_some_data(model_id)
    """Function in services."""
    return RelatedModel.objects(model=model_id).count()

class MyModelView(ModelView):
    column_formatters = {
        'related_model_count': lambda view, context, model, name: get_some_data(model.id)
    }    

    column_list = [..., 'related_model_count']

Remote model in Flask-Admin

| Comments

The task is to show a model from another application in Flask-Admin. A way to do it is to overwrite BaseModelView and get the data through HTTP request. Here is a simple example for a sortable list with a pagination.

Celery flower for several applications

| Comments

If there is a need to monitor several applications, there are two ways:

  • run celery flower instance for each application
celery flower --app app1.celeryapp --port=5555
celery flower --app app2.celeryapp --port=5556
  • run celery flower with broker option, there will be less options to control tasks
celery flower --broker=redis://redis/0

Celery checklist

| Comments

There is a good checklist to build great celery async tasks.

I want only to add how to autoreload celery worker in development mode. Before there was an --autoreload option, but now it’s removed. For this task I use watchdog.

pip install watchdog
watchmedo auto-restart --recursive -d app -p '*.py' -i '*.pyc' -- celery worker --app app.celeryapp --queues my_query -n my_queue@%h --loglevel INFO
1/4 ยป