OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to Use Asyncio to run 2 different async functions

  • Thread starter Thread starter elahren
  • Start date Start date
E

elahren

Guest
I am writing a script that uses the Graph API for Microsoft to connect to and query user data out of Entra ID in Azure. I have two different functions that I am trying to utilize, one that gets users from a specified group, and one that retrieves a user's display name given their ID value. If I use asyncio.run() to run either one or the other, both will run without issue. But if I try to have both in my script at once, I will get an error on the second one. I am still learning the intricacies of asynchronous programming so cannot for the life of me figure out why I can't get the second call of asyncio.run() to work.

I have this code in my main script that executes a function in another script:

Code:
async def get_all_group_members():
            memberListAll = []
            for group in groupList:
                # print(f"We are trying to run group {group}")
                results = await gq.get_group_user(group['GroupID'], graphServiceClient)
                memberDict = {"GroupName":group["GroupName"], "GroupID":group["GroupID"], "MemberList":results}
                memberListAll.append(memberDict)
            return memberListAll
results = asyncio.run(get_all_group_members())

Definition of get_group_user:

Code:
memberList = []
members = await graphServiceClient.groups.by_group_id(groupID).members.get()
     if members and members.value:
         for member in members.value:
             user = await graphServiceClient.users.by_user_id(member.id).get()
             if user:
                 memberList.append(user.display_name)
     return memberList

Then later in my main script, I have this second function that's referencing another function from the second script file:

Code:
async def get_user_name():
        results = await gq.get_user_by_id(member=member,graphServiceClient=graphServiceClient)
        return results

results2 = asyncio.run(get_user_name())

The defintion of get_user_by_id:

Code:
user = await graphServiceClient.users.by_user_id(member).get()
    print(user)

If I comment out everything for the second function, get_group_user, the first function will work just fine. If I comment out everything for the first function, get_all_group_members, the second function will work just fine. It's just when I have both actually run that the second one fails, and I don't know why so can't figure out how to fix it.

Why can I not call asyncio.run() more than once in a script? How do I change my script so that I can successfully run both of those functions asynchronously so I can get the data I need?


EDIT (figured out a solution myself):​


In order to be able to run both of the async functions, I created another async function that I put both of the other functions in, then called that one function using asyncio.run(). I still don't know why I can't call asyncio.run() two times in the same script without it causing issues, but at least I can move forward with this problem I am trying to solve. Here is what the code looks like now:

Code:
async def get_all_group_members():
        memberListAll = []
        for group in groupList:
            # print(f"We are trying to run group {group}")
            results = await gq.get_group_user(group['GroupID'], graphServiceClient)
            memberDict = {"GroupName":group["GroupName"], "GroupID":group["GroupID"], "MemberList":results}
            memberListAll.append(memberDict)
        return memberListAll
        
async def get_user_name():
        member = '<id value>'
        results = await gq.get_user_by_id(member=member,graphServiceClient=graphServiceClient)
        return results
        
async def run_async_functions():
        groupResults = await get_all_group_members()
        usernameResults = await get_user_name()
        return groupResults, usernameResults
            
groupResults, usernameResults = asyncio.run(run_async_functions())

After loading the results of both functions into groupResults and usernameResults, I am then able to print out the values, which means I'll be able to interact with them in the way I need to moving forward.
<p>I am writing a script that uses the Graph API for Microsoft to connect to and query user data out of Entra ID in Azure. I have two different functions that I am trying to utilize, one that gets users from a specified group, and one that retrieves a user's display name given their ID value. If I use asyncio.run() to run either one or the other, both will run without issue. But if I try to have both in my script at once, I will get an error on the second one. I am still learning the intricacies of asynchronous programming so cannot for the life of me figure out why I can't get the second call of asyncio.run() to work.</p>
<p>I have this code in my main script that executes a function in another script:</p>
<pre><code>async def get_all_group_members():
memberListAll = []
for group in groupList:
# print(f"We are trying to run group {group}")
results = await gq.get_group_user(group['GroupID'], graphServiceClient)
memberDict = {"GroupName":group["GroupName"], "GroupID":group["GroupID"], "MemberList":results}
memberListAll.append(memberDict)
return memberListAll
results = asyncio.run(get_all_group_members())
</code></pre>
<p>Definition of get_group_user:</p>
<pre><code>memberList = []
members = await graphServiceClient.groups.by_group_id(groupID).members.get()
if members and members.value:
for member in members.value:
user = await graphServiceClient.users.by_user_id(member.id).get()
if user:
memberList.append(user.display_name)
return memberList
</code></pre>
<p>Then later in my main script, I have this second function that's referencing another function from the second script file:</p>
<pre><code>async def get_user_name():
results = await gq.get_user_by_id(member=member,graphServiceClient=graphServiceClient)
return results

results2 = asyncio.run(get_user_name())
</code></pre>
<p>The defintion of get_user_by_id:</p>
<pre><code>user = await graphServiceClient.users.by_user_id(member).get()
print(user)
</code></pre>
<p>If I comment out everything for the second function, <code>get_group_user</code>, the first function will work just fine. If I comment out everything for the first function, <code>get_all_group_members</code>, the second function will work just fine. It's just when I have both actually run that the second one fails, and I don't know why so can't figure out how to fix it.</p>
<p>Why can I not call asyncio.run() more than once in a script? How do I change my script so that I can successfully run both of those functions asynchronously so I can get the data I need?</p>
<hr />
<h2>EDIT (figured out a solution myself):</h2>
<p>In order to be able to run both of the async functions, I created <em>another</em> async function that I put both of the other functions in, then called that one function using asyncio.run(). I still don't know <em>why</em> I can't call asyncio.run() two times in the same script without it causing issues, but at least I can move forward with this problem I am trying to solve. Here is what the code looks like now:</p>
<pre><code>async def get_all_group_members():
memberListAll = []
for group in groupList:
# print(f"We are trying to run group {group}")
results = await gq.get_group_user(group['GroupID'], graphServiceClient)
memberDict = {"GroupName":group["GroupName"], "GroupID":group["GroupID"], "MemberList":results}
memberListAll.append(memberDict)
return memberListAll

async def get_user_name():
member = '<id value>'
results = await gq.get_user_by_id(member=member,graphServiceClient=graphServiceClient)
return results

async def run_async_functions():
groupResults = await get_all_group_members()
usernameResults = await get_user_name()
return groupResults, usernameResults

groupResults, usernameResults = asyncio.run(run_async_functions())
</code></pre>
<p>After loading the results of both functions into <code>groupResults</code> and <code>usernameResults</code>, I am then able to print out the values, which means I'll be able to interact with them in the way I need to moving forward.</p>
 

Latest posts

Online statistics

Members online
0
Guests online
5
Total visitors
5
Top