I'm relatively new to Python and having a go at developing a bot for Discord. I can get the bot working but I'm having trouble with a couple of new commands I put in.
I've created a function called "Member_City_Count" that parses json from a site using urllib.requests and stores the data in a file. I know that works on it's own, however when the bot is called to run it I get the following error after the function has finished running and the bot shuts down:
(Task was destroyed but it is pending! task:
Task pending coro=<_run_event()running at C:\Users\dom\AppData\Local\Programs\Python\
Python35-32\lib\site_packages\discord\client.py:307>wait_for=Future pending
cb=[Task._wakeup(), BaseSelectorEventLoop._sock_connect_done(964)()]>)
What confuses me most is that about 1 in 4 attempts to run it will be successful!
async def on_message(message):
if message.content.startswith(!Cities):
await client.send_message(message.channel, "Collecting Data ...")
Member_City_Count()
await client.send_message(message.channel, "Complete")
Apologies if the issue is glaringly obvious, still trying to get to grips with asyncio.
The discord api needs to ping the server every N seconds (N is about 60).
If this doesn't happen, you will get an exception at some point (I believe upon next discord api call).
If you have a long running function anywhere in your workflow, this will prevent a ping and cause your client to enter a bad state.
Remember that Python is essentially 'single core' during CPU intensive tasks.
This is related to asyncio, but it's more to do with the specific usage pattern of discord.
You could offload your heavy blocking processing into another process; else split it up into many tiny async functions.
Here's some vaguely related info: https://discordpy.readthedocs.io/en/latest/faq.html#what-does-blocking-mean