Make a Python asyncio call from a Flask route

前端 未结 5 594
迷失自我
迷失自我 2020-12-07 15:31

I want to execute an async function every time the Flask route is executed. Why is the abar function never executed?

import asyncio
from flask          


        
5条回答
  •  粉色の甜心
    2020-12-07 16:19

    You can incorporate some async functionality into Flask apps without having to completely convert them to asyncio.

    import asyncio
    from flask import Flask
    
    async def abar(a):
        print(a)
    
    loop = asyncio.get_event_loop()
    app = Flask(__name__)
    
    @app.route("/")
    def notify():
        loop.run_until_complete(abar("abar"))
        return "OK"
    
    if __name__ == "__main__":
        app.run(debug=False, use_reloader=False)
    

    This will block the Flask response until the async function returns, but it still allows you to do some clever things. I've used this pattern to perform many external requests in parallel using aiohttp, and then when they are complete, I'm back into traditional flask for data processing and template rendering.

    import aiohttp
    import asyncio
    import async_timeout
    from flask import Flask
    
    loop = asyncio.get_event_loop()
    app = Flask(__name__)
    
    async def fetch(url):
        async with aiohttp.ClientSession() as session, async_timeout.timeout(10):
            async with session.get(url) as response:
                return await response.text()
    
    def fight(responses):
        return "Why can't we all just get along?"
    
    @app.route("/")
    def index():
        # perform multiple async requests concurrently
        responses = loop.run_until_complete(asyncio.gather(
            fetch("https://google.com/"),
            fetch("https://bing.com/"),
            fetch("https://duckduckgo.com"),
            fetch("http://www.dogpile.com"),
        ))
    
        # do something with the results
        return fight(responses)
    
    if __name__ == "__main__":
        app.run(debug=False, use_reloader=False)
    

提交回复
热议问题