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 = {
'Authorization': 'Client-ID {}'.format(self.client_id)
}
data = {
'image': open("/home/jibin/Downloads/test.jpg", "rb")
}
async def upload(data):
async with aiohttp.ClientSession() as session:
async with session.post(self.url, headers=header,data=data) as resp:
data = await resp.text()
print(data)
futures = []
futures.append(upload(data))
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(futures))
loop.close()
return response.json("done",status=200)
this is how I call the request from the route.
@app.route('/upload', methods=['POST'])
async def upload(request):
return up(request)
However, it returns RuntimeError: this event loop is already running. error
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…