Python Forum
Issues with async and yielding from API
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issues with async and yielding from API
#1
Heyo guys,

So currently i'm trying to get a Discord bot to work, which uses asyncio, and an API which uses a yield function inside an event loop.

I'm trying to get a value from an api and the line on 17 does actually print a value, however at line 41 it sais that ret_mess is null.

I'm not sure what to do at this point:

import discord import asyncio import r6sapi as api TOKEN = 'XXX' client = discord.Client() auth = api.Auth('XXX', 'XXX') ret_mess = ""; @asyncio.coroutine def get_kills(name, operator): global ret_mess player = yield from auth.get_player("TryingDutchman_", api.Platforms.UPLAY) operator = yield from player.get_operator("hibana") print(operator.kills) ret_mess = str(operator.kills) def get_player(name): player = yield from auth.get_player(name, api.Platforms.UPLAY) operator = player.__next__().get_operator("sledge"); return operator; @client.event async def on_message(message): global ret_mess if message.author == client.user: return if message.content.startswith('!hello'): msg = 'Hello {0.author.mention}'.format(message) await client.send_message(message.channel, msg) if message.content.startswith('!update'): try: asyncio.get_event_loop().run_until_complete(get_kills(message.author.display_name, "hibana")) except: print("ret: " + ret_mess) pass await client.send_message(message.channel, ret_mess) @client.event async def on_ready(): global auth print('Logged in as') print(client.user.name) print('------') client.run(TOKEN)
Reply
#2
Well, the only way it would say ret: null would be from line 39, in the error handler. So instead of using a catchall error handler, print the actual error.

except Exception as e: print(e) print("ret: " + ret_mess)
The harder, but "better" answer, would be to stop using globals. My guess is you shouldn't be calling run_until_complete() inside an event loop. That whole line should probably be ret_mess = await get_kills(message.author.display_name, "hibana"). And get_kills() should probably return the ret_mess, instead of (or in addition to) setting a global variable.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Shocked async was never was never awaited but the await is still in the code Nietzsche 3 2,003 Jun-04-2024, 02:28 PM
Last Post: deanhystad
  get data from 2 async functions korenron 0 2,026 Sep-22-2021, 08:39 AM
Last Post: korenron
Question Yielding ahead of time Daring_T 5 4,033 Jun-01-2021, 02:43 AM
Last Post: Daring_T
  Async requests lukee 0 2,223 Oct-06-2020, 04:40 AM
Last Post: lukee
  Async IO writing to two Different Tables Help TiagoV 0 3,734 Oct-09-2019, 04:45 AM
Last Post: TiagoV
  async question on raspberry pi baukeplugge 2 100,766 Nov-07-2018, 07:58 PM
Last Post: baukeplugge
  Async server/client Bokka 2 5,318 May-29-2017, 03:09 PM
Last Post: Bokka

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.