将 telethon 与 django 结合使用:线程“Thread-1”中没有当前事件循环

Sas*_*deH 2 python django multithreading asynchronous telethon

我想使用django中的Telethon。但是当我运行它时,我收到以下错误:

RuntimeError:线程“Thread-1”中没有当前事件循环。

我的代码views.py:

from django.shortcuts import render,HttpResponse
from telethon.sync import TelegramClient, events

async def join(client):
    ch = '@andeh_ir'
    try:
        await client(JoinChannelRequest(ch))
        print('[+] Joined The Channel')
    except:
        print('[-] skiped')

def addChannel(request):
    api_id   =   XXXXXX
    api_hash = 'xxxxxxxxxxxxxxxxxxxxx'
    client = TelegramClient('+254716550762', api_id, api_hash )
    with client:
        client.loop.run_until_complete(join(client))
    return HttpResponse('addChannel')
Run Code Online (Sandbox Code Playgroud)

小智 6

我的解决方案#import asyncio

 import asyncio
 api_id = xxxx
 api_hash = 'b772662cf8a918bfbc39ee2aee36f6cc'
 loop = asyncio.new_event_loop()
 asyncio.set_event_loop(loop)
 client = TelegramClient(phone, api_id, api_hash, loop=loop)
Run Code Online (Sandbox Code Playgroud)