Skip to content Skip to sidebar Skip to footer

Runtimeerror: This Event Loop Is Already Running

I am trying to run an asynchronous 3rd party file upload using the following code in sanic def up(self,request): import asyncio import aiohttp header = { 'Aut

Solution 1:

Here is the code that worked for me in sanic

@app.route('/upload')
async def get_ressource(request):
    asyncio.ensure_future(blocking_function())
    return await resp()

async def blocking_function():
    async with aiohttp.ClientSession() as session:
        async with session.post("your_url", data={
            'image': open("file_path", "rb"),
        }, headers={
            'Authorization': 'Client-ID {}'.format("your_client_id")
        }) as resp:
            result = await resp.text()
    print(result)


async def resp():
    return response.json("OK", status=202)

Post a Comment for "Runtimeerror: This Event Loop Is Already Running"