I’m coding a telegram userbot (with telethon) which sends a message,every 60 seconds, to some chats.
I’m using 2 threads but I get the following errors: "RuntimeWarning: coroutine ‘sender’ was never awaited" and "no running event loop".
My code:
....
async def sender():
for chat in chats :
try:
if chat.megagroup == True:
await client.send_message(chat, messaggio)
except:
await client.send_message(myID, 'error')
schedule.every(60).seconds.do(asyncio.create_task(sender()))
...
class checker1(Thread):
def run(self):
while True:
schedule.run_pending()
time.sleep(1)
class checker2(Thread):
def run(self):
while True:
client.add_event_handler(handler)
client.run_until_disconnected()
checker2().start()
checker1().start()
I searched for a solution but I didn’t find anything…
2
Answers
There are a few problems with your code.
asyncio
is complaining about "no running event loop" because your program never starts the event loop anywhere, and tasks can’t be scheduled without an event loop running. See Asyncio in corroutine RuntimeError: no running event loop. In order to start the event loop, you can useasyncio.run_until_complete()
if you have a main coroutine for your program, or you can useasyncio.get_event_loop().run_forever()
to run the event loop forever.The second problem is the incorrect usage of
schedule.every(60).seconds.do()
, which is hidden by the first error.schedule
expects a function to be passed in, not an awaitable (which is whatasyncio.create_task(sender())
returns). This normally would have caused aTypeError
, but thecreate_task()
without a running event loop raised an exception first, so this exception was never raised. You’ll need to define a function and then pass it toschedule
, like this:This should work as long as the event loop is started somewhere else in your program.
You should avoid using threads with
asyncio
unless you know what you’re doing. The code can be rewritten usingasyncio
as follows, since most of the time you don’t actually need threads:This code is not perfect (you should properly cancel and wait
checker1
at the end of the program), but it should work.As a side note, you don’t need
client.run_until_disconnected()
. The call simply blocks (runs) until the client is disconnected. If you can keep the program running differently, as long asasyncio
runs, theclient
will work.Another thing: bare
except:
are a very bad idea, and will probably cause issues with exception. At least, replace it withexcept Exception
.