Search code examples
pythondjangoasynchronousasync-awaittelegram

Asynchronous tasks in Django


I want to implement a telegram group parser (namely users) into the Django application. The idea is this, from the post request I get the name of the group entered into the form, I pass it to the asynchronous function (which lies in a separate file) and parsing starts. And so at a call of asynchronous function in view, constantly I receive errors. Such as "RuntimeError('The asyncio event loop must not change after connection", "RuntimeError: There is no current event loop in thread 'Thread-1'" etc. Please help me, how would this be better organized?

Function for parsing users below:

api_id =  **** 
api_hash = '*****' 
phone = '+******' 
client = TelegramClient('+********', api_id, api_hash)

client.start()


async def dump_all_participants(url):
    """Writes a csv file with information about all channel/chat participants"""
    offset_user = 0    # member number from which reading starts
    limit_user = 200   # maximum number of records transferred at one time

    all_participants = []   # list of all channel members
    filter_user = ChannelParticipantsSearch('')

    
    while True:
        participants = await client(GetParticipantsRequest(await client.get_entity(url),
            filter_user, offset_user, limit_user, hash=0))
        if not participants.users:
            break
        all_participants.extend(participants.users)
        offset_user += len(participants.users)

    all_users_details = []   # list of dictionaries with parameters of interest to channel members
    for participant in all_participants:
        all_users_details.append({"id": participant.id,
            "first_name": participant.first_name,
            "last_name": participant.last_name,
            "user": participant.username,
            "phone": participant.phone})
    
    df =  pd.DataFrame(all_users_details)
    csv_file_path = './data.csv'
    df.to_csv(csv_file_path, index=False)

Solution

  • You encountered the Django thread problem because calling an async function as you do opens a new thread that is occupied by Django. The problem is, you can't really mix Telethon code without using async views, and I'm not certainly sure if the issue will persists with Celery, so I really recommend upgrading to Django 4 version and try to use async views with ASGI.