I want to create simple async web app
the app.py looks like this
async def get_favicon(link: str):
return await _get_favicon(link)
async def handle(request):
url: Optional[str] = request.query['url']
result = await get_favicon(url)
return web.json_response(result, status=200)
async def init():
app = web.Application()
app.router.add_get("/", handle)
return app
if __name__ == "__main__":
application = init()
web.run_app(application, port=8000)
Inside of get_favicon i have a _get_favicon function
async def _get_favicon(self, url: str, biggest: bool = True, **request_kwargs) -> Dict:
request_kwargs.setdefault('headers', self.HEADERS)
request_kwargs.setdefault('allow_redirects', True)
request_kwargs.setdefault('verify', False)
response = await requests_async.get(url, **request_kwargs)
response.raise_for_status()
icons = set()
default_icon = await default(response.url, **request_kwargs)
and default fucntion
async def default(url: str, **request_kwargs: Dict) -> Optional[Icon]:
parsed = urlparse(url)
favicon_url = urlunparse((parsed.scheme, parsed.netloc, 'favicon.ico', '', '', ''))
response = await requests_async.head(favicon_url, **request_kwargs)
if response.status_code == 200:
return Icon(response.url, 0, 0, 'ico')
When i try to run it, i got RuntimeError: This event loop is already running in default function at response = await requests_async.head(favicon_url, **request_kwargs). I don't understand when i got wrong. I don't create another loop. If i run default_icon = self.default(response.url, **request_kwargs) without await i ll get coroutine object which is not exactly what i am looking for. I also tried to default_icon = await asyncio.gather(default(response.url, **request_kwargs)) and got the same error.