Issue
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)
Solution
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.
Answered By - Nilay Sheth
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.