Home > other >  Asyncio set event loop make freeze
Asyncio set event loop make freeze

Time:12-26

I am creating an application in python and i need to create channel with telegram, for make this, i use telethon:

async def createChannel(username, desc):
try:
    chn = await client(CreateChannelRequest(username,desc,megagroup=False))
    new_channel_id = chn.chats[0].id
    new_channel_access_hash = chn.chats[0].access_hash
    update_response = await client(UpdateUsernameRequest(
            InputPeerChannel(channel_id=new_channel_id,
                            access_hash=new_channel_access_hash), username))
except:
    await client.connect()
    chn = await client(CreateChannelRequest(username,desc,megagroup=False))
    new_channel_id = chn.chats[0].id
    new_channel_access_hash = chn.chats[0].access_hash
    update_response = await client(UpdateUsernameRequest(
            InputPeerChannel(channel_id=new_channel_id,
                            access_hash=new_channel_access_hash), username))
return update_response

I want to use multi threading but when i do that, i got error that say me "No event loop in the thread", so ask google and he gave me this:

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

But when i have this and my multi threading, it just stuck and do nothing at this line:

chn = await client(CreateChannelRequest(username,desc,megagroup=False))

Any idea? thx :)

CodePudding user response:

When initializing the client object, you should pass the event loop to TelegramClient like:

from telethon import TelegramClient

loop = asyncio.new_event_loop()
client = TelegramClient(..., loop=loop)
  • Related