1

I wanted to run Telethon events in multi thread, multithreading.

For the following code, what I expected is multi thread, cocurrent independent events. await asyncio.sleep( 3 ) will sleep 3 seconds. So If you send any text on telegram to the account, it will wait 3 seconds and send me back "hi.". I expected that if I send a text for several times at once, let's say 5 times, it should wait for approximately 3 seconds and send me "hi" back at once, but not like wait for 3 seconds, send "hi.", wait for 3 seconds, send "hi." .. for 5 times and takes approximately 15 seconds. This is not what I expected for.

So, How can I modify the code to run the code as expected? Or it's impossible in telethon?

I read google, stackoverflow, github issue, but couldn't find a solution by myself. So I want your help here. Thank you.

import time from pathlib import Path import re import random import asyncio from telethon import TelegramClient, events from telethon.sessions import StringSession from telethon import utils import logging logging.basicConfig(level=logging.ERROR) api_id = "" api_hash = '' phone = '' string = Path('./string').read_text() message = "hi." if __name__ == '__main__': client = TelegramClient(StringSession(string), api_id, api_hash, sequential_updates=True) @client.on(events.NewMessage(incoming=True)) async def handle_new_message(event): print('message:', event.message.message) # // not working here, the sleep blocks next events but not only current event. print('wait for 3 seconds...') await asyncio.sleep( 3 ) await event.respond(message) print('Auto-replying...') client.start(phone) client.run_until_disconnected() # client.loop.run_forever() # string = StringSession.save(client.session) # Path('./string').write_text(string) 
2
  • @TheKill-996 you mean the code worked as expected? my python version is 3.6.7, and Telethon==1.18.2, tell me, what is your versions? thanks. Commented Dec 12, 2020 at 11:38
  • FWIW Telethon is not multi-threaded, it just uses multiple tasks by default to handle updates. Commented Dec 12, 2020 at 18:50

1 Answer 1

2

You are using sequential_updates so until the first update is not completed it will not move to other update. I guess you need to turn off sequential updates.

Sign up to request clarification or add additional context in comments.

1 Comment

oh my god, so the answer was already there. my bad, should have read the code more carefully. you're a lifesaver, thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.