Checklist
pip install -U https://github.com/LonamiWebs/Telethon/archive/master.zip and triggered the bug in the latest version.Code that causes the issue
from telethon.sync import TelegramClient
import ...
...
class TgWorker:
def __init__(self, tg_id, tg_hash, number, session_name):
self.client = TelegramClient(session_name, tg_id, tg_hash)
self.client.start()
def get_message(self, channel_id):
channel = self.client.get_entity(channel_id)
message = self.client.get_messages(channel, 1)
return ...
class Worker(QThread):
def __init__(...):
self.tg = TgWorker(...)
def get_info(...):
message = self.tg.get_message(...)
def run(...):
info = self.get_info(...)
class App(QtWidgets.QMainWindow, gui.Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.info_thread = Worker(...) # creating new thread
def start_thread_action(self):
self.info_thread.start()
def main():
app = QApplication(sys.argv)
window = App()
window.start_thread_action()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Traceback
Traceback (most recent call last):
File "D:/Python/ProjectName/ProjectFile.py", line 577, in run
info = self.get_info()
File "D:/Python/ProjectName/ProjectFile.py", line 325, in get_info
message = self.tg.get_message(...)
File "D:/Python/ProjectName/ProjectFile.py", line 315, in get_message
channel = self.client.get_entity(channel_id)
File "D:\Python\Anaconda3\lib\site-packages\telethon\sync.py", line 35, in syncified
loop = asyncio.get_event_loop()
File "D:\Python\Anaconda3\lib\asyncio\events.py", line 678, in get_event_loop
return get_event_loop_policy().get_event_loop()
File "D:\Python\Anaconda3\lib\asyncio\events.py", line 584, in get_event_loop
% threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'Dummy-1'.
Yes, i know that Threads with Asyncio are not good, i've spent two days in search of solution. Previous solution i've found here was in using telethon-sync module, but then I've found information from you that it has been removed. In my app I have to use threads, because one is for user buttons, another is for bot, who's goal is to get last messages from telegram using client.
Firstly, i've tested code connected with only telethon in another file with only one thread, which is getting messages from channel and everything worked good.
Maybe, telethon-sync is available or maybe you know, what to change in this module. (i've tried to change some code in sync.py, but it was stupid solution with creating new loop and setting then. And ofc i've got error with starting new loop while another is running.
Thank you!
- [x] The error is in the library's code, and not in my own.
Why did you tick that box? It is not an error inside the library. Your code is faulty. You have to either learn by yourself how to mix threads and asyncio or learn basic asyncio (create several tasks…), because this problem does not require threads at all, and this is the wrong place to ask questions.
Why so rude?
If someone has the same question: https://docs.telethon.dev/en/latest/concepts/asyncio.html?highlight=thread#can-i-use-threads
Why so rude?
Sorry, could you explain how my comment above was rude in any way?
Nowhere in the message you can see a sign of disrespect. Only straight facts.
I'm also not sure why you've revived like 4 ancient issues on the topic when there has been no recent activity on them.