You can use asyncio’s coroutine [1] in flask’s view function using this extension.

from flask.ext.aiohttp import async

@async  # It marks view function as asyncio coroutine
def late_response():
    yield from asyncio.sleep(3)
    return "Sorry, I'm late!"

So, you can use aiohttp’s request modules in flask.

from flask.ext.aiohttp import async

def zuck():
    response = yield from aiohttp.request(
        'GET', '')
    data = yield from

    return data

And you can surely use flask’s common feature.

import json
import urllib.parse

from flask import current_app, request
from flask.ext.aiohttp import async

def facebook_profile(name):
    if request.args.get('secure', False):
        url = ''
        url = ''
    url = url + urllib.parse.quote(name)
    response = yield from aiohttp.request('GET', url)
    data = yield from
    data = json.loads(data)

    def stream():
        if request.args.get('wrap', False):
            data = {
                'data': data
        yield json.dumps(data)
    return current_app.response_class(stream())


Since coroutine implemented by using streaming response, you have to be care about using request hook.

before_request(), after_request(), teardown_request() will be called twice.

Each asynchronous request’s functions will be called in following sequence.

  1. before_request()
  2. Flask-aiohttp’s streaming response containing coroutine
  3. after_request()
  4. teardown_request()

Streaming response starts here

  1. before_request()
  2. Your coroutine response
  3. after_request()
  4. teardown_request()