0

I would like to run ReturnViews function in a separate thread. Telethon is async library, so I am getting error: RuntimeError: There is no current event loop in thread 'ThreadPoolExecutor-0_0'. How can I solve it?

from apscheduler.schedulers.background import BackgroundScheduler
from telethon.sync import TelegramClient
from telethon import functions
import telebot

token = '19082138:AAH6keFlK-ak'
api_id = 91145
api_hash = '05c6f86e67ea7e515ad7615a8'
bot = telebot.TeleBot(token)


def ReturnViews():
    client = TelegramClient("name", api_id, api_hash)
    result = client(functions.messages.GetMessagesViewsRequest(
        peer="me",
        id=1,
        increment=False
    ))
    return result.views[0].views

def background_schedule():
    scheduler = BackgroundScheduler()
    scheduler.add_job(ReturnViews, 'interval', seconds=3)
    scheduler.start()

    bot.polling(none_stop=True)


if __name__ == '__main__':
    print("Hello ")
    background_schedule()
  • You can read https://stackoverflow.com/q/28492103/ for insight on how to use `asyncio` and `threading` together, however, it's easier if you just use `asyncio` for everything. – Lonami Jul 31 '21 at 14:18

0 Answers0