5

Lately been reading about python concurrency realpython - python concurrency

My main focus asyncio so am fairly new.

The code block that performs asynchronous activities using asyncio and aiohttp runs fine when run it directly.

However when i add the code to my flask blueprint it raises this error:

RuntimeError: There is no current event loop in thread 'Thread-2'

For demonstration purposes i made a demo flask app. In case anyone wants to test it out.

main.py

from flask import Flask
from my_blueprint import my_blueprint

#Define flask app
app = Flask(__name__)

#load blueprints
app.register_blueprint(my_blueprint,url_prefix='/demo')

#start flask
if __name__ == '__main__':
    app.run(debug=True)

my_blueprint.py

from flask import Blueprint,request, jsonify,abort,make_response
from flask import make_response
import asyncio
import time
import aiohttp

my_blueprint = Blueprint('my_blueprint', __name__)

@my_blueprint.route('/',methods=['GET'])
def home():
    #code block
    async def download_site(session, url):
        async with session.get(url) as response:
            print("Read {0} from {1}".format(response.content_length, url))


    async def download_all_sites(sites):
        async with aiohttp.ClientSession() as session:
            tasks = []
            for url in sites:
                task = asyncio.ensure_future(download_site(session, url))
                tasks.append(task)
            await asyncio.gather(*tasks, return_exceptions=True)

    sites = ["https://www.jython.org","http://olympus.realpython.org/dice"]*20
    start_time = time.time()
    asyncio.get_event_loop().run_until_complete(download_all_sites(sites))
    duration = time.time() - start_time
    return jsonify({"status":f"Downloaded {len(sites)} sites in {duration} seconds"})
    #end of code block

1 Answer 1

7

EDIT: It looks like your code was correct. I am used to writing it different. But you are probably running windows and python 3.8. that just changed the default event loop policy in python 3.8 on windows and it pretty buggy. You can change back the old event loop policy:

change:

asyncio.get_event_loop().run_until_complete(download_all_sites(sites))

into:

asyncio.set_event_loop(asyncio.SelectorEventLoop())
asyncio.get_event_loop().run_until_complete(download_all_sites(sites))
Sign up to request clarification or add additional context in comments.

5 Comments

What was the issue...can you explain a bit more
There are also a bunch of errors when you refresh page http://127.0.0.1:5000/demo/ on browser raise RuntimeError('Event loop is closed')
I was not getting any errors on a refresh. But i think its probably a bug with the new eventloop policy in windows & python 3.8+. I seen alot of stuf error out until you switch the eventloop policy back to the old one. (i edited my answer)
I see will have a look and get back to you... someone accidentally formatted my linux partion...had to test on windows(reinstalling Linux now)...if it works on Linux that's probably fine.
Fixed the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.