0

I was trying to use async on Azure to speed up the processing time but it gave me an error when I ran it, which is RuntimeError: asyncio.run() cannot be called from a running event loop

Here is my code, the inputs are azure client, and a list of documents.

import os
import asyncio
import pandas as pd
from azure.ai.textanalytics import TextAnalyticsClient
from azure.core.credentials import AzureKeyCredential

ace = [{'doc_name': 'doc_1',
        'mention': 'Chris Evans',
        'title': 'Chris Evans',
        'full_text': "Chris Evans started as human torch......."},
        {'doc_name': 'doc_2',
        'mention': 'Tony Stark',
        'title': 'Chris Evans',
        'full_text': "Tony Stark is ironman which likes......."},
        {'doc_name': 'doc_3',
        'mention': 'Chris Evans',
        'title': 'Thor',
        'full_text': "Thor loves to drink......."},
        ....... more docs......
        }]

key = "my-key"
endpoint = "my-endpoint"

def authenticate_client():
    ta_credential = AzureKeyCredential(key)
    text_analytics_client = TextAnalyticsClient(
            endpoint=endpoint, 
            credential=ta_credential)
    return text_analytics_client

client = authenticate_client()

async def entity_linking(client, doc):
    
    name = []
    i_d = []
    url = []
    temp = []
    text = []
    doc_n = []
    
    for i in range(len(doc)):

        try:
            documents = [doc[i]['full_text']]
            
            async with client:
                
                result = await client.recognize_linked_entities(documents = documents)[0]

    #         print("Linked Entities:\n")
            for entity in result.entities:
                doc_n.append(doc[i]['doc_name'])
                name.append(entity.name)
                i_d.append(entity.data_source_entity_id)
                url.append(entity.url)
                temp.append(entity.matches)

        except:
            doc_n.append('')
            name.append('')
            i_d.append('')
            url.append('')
            text.append('')
            
            
    for i in range(len(temp)):
        text.append(temp[i][0]['text'])
        
    dataframe = pd.DataFrame({
        'doc_name': doc_n,
        'name': name,
        'id': i_d,
        'text': text,
        'wiki_url': url
    })
        
        
    return dataframe
#     except Exception as err:
#         print("Encountered exception. {}".format(err))


async def main():
    await entity_linking2(client,ace)

if __name__ == '__main__':
    asyncio.run(main())

I tried to run it with my code with async above but received an error RuntimeError: asyncio.run() cannot be called from a running event loop. I am wondering did I set it up wrong in my code above or some other problems. Also, if I am intered in getting the latency for Azure when I run the code above, are there any build in functions for Azure for me to get it? Thanks everyone!

CodingStark
  • 199
  • 3
  • 17

1 Answers1

0

The documentation for asyncio.run() states:

When another asyncio event loop is running in the same thread, this function cannot be called.

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

Even if your code is outside of any asynchronous function, you don't need to start the event loop yourself and can instead call await main(url) directly.

References :