Need help with Flask-aiohttp?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

Hardtack
129 Stars 12 Forks MIT License 10 Commits 3 Opened issues

Description

Asynchronous Flask using aiohttp

Services available

!
?

Need anything else?

Contributors list

# 166,668
React
npm
ethereu...
mdc
6 commits
# 449,423
C++
C
Shell
xbmc
1 commit
# 191,692
Shell
C++
Jupyter...
C
1 commit

Flask-aiohttp --- Asynchronous Flask using aiohttp.

.. image:: http://unmaintained.tech/badge.svg :target: http://unmaintained.tech/ :alt: No Maintenance Intended

EXPERIMENTAL

I made this project for testing compatability between WSGI & Async IO.

Since WSGI has no consideration of Async IO, Flask-aiohttp cannot be perfect.

So, I don't recommend you to use this library for production. Libraries that was made for Async IO would be better choice (Like gevent, Tornado or AioHTTP).

Features

  • Coroutine view function

    You can make view function to asyncio coroutine using :func:

    async
    . ::
    @app.route('/slow')
    @async
    def slow():
        yield from asyncio.sleep(3)
        return 'sorry!'
    
  • Asynchronous I/O in view function

    You can do asynchronous I/O in your view function. ::

    @app.route('/zuck')
    @async
    def zuck():
        response = yield from aiohttp.request(
            'GET', 'https://graph.facebook.com/zuck')
        data = yield from response.read()
        return data
    
  • Websocket

    You can use aiohttp's WebSocketResponse in your Flask view function. ::

    @app.route('/echo')
    @websocket
    def echo():
        while True:
            msg = yield from aio.ws.receive_msg()
    
    
        if msg.tp == aiohttp.MsgType.text:
            aio.ws.send_str(msg.data)
        elif msg.tp == aiohttp.MsgType.close:
            print('websocket connection closed')
            break
        elif msg.tp == aiohttp.MsgType.error:
            print('ws connection closed with exception %s',
                  aio.ws.exception())
            break

  • Flask URL routing

    It surely compatible with flask's view routing. ::

    @app.route('/param/')
    @websocket
    def param(arg):
        while True:
            msg = yield from aio.ws.receive_msg()
    
    
        if msg.tp == aiohttp.MsgType.text:
            aio.ws.send_str(arg)
        elif msg.tp == aiohttp.MsgType.close:
            print('websocket connection closed')
            break
        elif msg.tp == aiohttp.MsgType.error:
            print('ws connection closed with exception %s',
                  aio.ws.exception())
            break

Usage

You can use it just like plain Flask extensions. ::

import flask
from flask.ext.aiohttp import AioHTTP

app = flask.Flask(name) aio = AioHTTP(app)

or ::

aio = AioHTTP()
aio.init_app(app)

But, you have to run the application using our runner to be run asynchronously. ::

if __name__ == '__main__':
    aio.run(app, debug=True)

Documentation

Read the docs

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.