0
url1=['www.google.com', 'www.youtube.com', 'www.facebook.com']
async def dnsRec(url):
    async with whois.whois(urlparse(url).netloc) as response:
        domain_detail = response.json()
        return domain_detail
    
async def main():
    task = [dnsRec(i) for i in url1]
    await asyncio.gather(*task)
    
def func():
    asyncio.run(main())

func()

RuntimeError: asyncio.run() cannot be called from a running event loop

Venkat
  • 1
  • 1
  • 4

1 Answers1

2

One solution could be that you are already using a framework such as FastAPI which already creates an async queue. Therefore you need to add your async function to the existing loop. For instance:

# ... previous code

loop = asyncio.get_event_loop()

def func():
    asyncio.run(main())

loop.create_task(func())
carkod
  • 1,844
  • 19
  • 32