gpt4 book ai didi

python - 运行时错误: Task got Future attached to a different loop

转载 作者:行者123 更新时间:2023-12-01 00:49:26 27 4
gpt4 key购买 nike

如何在Quart中的另一个异步方法中调用在主线程中获取事件循环的异步方法?

t.py

from telethon import TelegramClient, functions, types

client2 = TelegramClient(sn, api_id, api_hash).start()

async def create_contact():
return await client2(functions.contacts.ImportContactsRequest([
types.InputPhoneContact(0, '8', 'first_name', 'last_name')
]))

应用程序.py

from quart import Quart, websocket,render_template,request
import t2
app = Quart(__name__)

@app.route('/wa2tg')
def wa2tg():
return render_template('wa2tg.html',nm=request.args.get('nm',''))

@app.websocket('/wa2tg2')
async def wa2tg2():
while True:
data = await websocket.receive()
await t2.create_contact()

# Thread(target=tele.client2.run_until_disconnected).start()
app.run(debug=1)

错误:

Running on http://127.0.0.1:5000 (CTRL + C to quit)
[2019-06-21 16:31:42,035] 127.0.0.1:51696 GET /wa2tg 1.1 200 553 12995
[2019-06-21 16:31:42,486] 127.0.0.1:51698 GET /wa2tg2 1.1 101 - 999
[2019-06-21 16:31:42,490] ERROR in app: Exception on websocket /wa2tg2
Traceback (most recent call last):
File "C:\Users\Smart\AppData\Local\Programs\Python\Python37-32\lib\site-packages\quart\app.py", line 1629, in handle_websocket
return await self.full_dispatch_websocket(websocket_context)
File "C:\Users\Smart\AppData\Local\Programs\Python\Python37-32\lib\site-packages\quart\app.py", line 1651, in full_dispatch_websocket
result = await self.handle_user_exception(error)
File "C:\Users\Smart\AppData\Local\Programs\Python\Python37-32\lib\site-packages\quart\app.py", line 948, in handle_user_exception
raise error
File "C:\Users\Smart\AppData\Local\Programs\Python\Python37-32\lib\site-packages\quart\app.py", line 1649, in full_dispatch_websocket
result = await self.dispatch_websocket(websocket_context)
File "C:\Users\Smart\AppData\Local\Programs\Python\Python37-32\lib\site-packages\quart\app.py", line 1694, in dispatch_websocket
return await handler(**websocket_.view_args)
File "D:\SmartBot\my_env\SmartBot\t.py", line 13, in wa2tg2
await t2.create_contact()
File "D:\SmartBot\my_env\SmartBot\t2.py", line 22, in create_contact
types.InputPhoneContact(0, '8807328487', 'first_name', 'last_name')
File "C:\Users\Smart\AppData\Local\Programs\Python\Python37-32\lib\site-packages\telethon\client\users.py", line 60, in __call__
result = await future
RuntimeError: Task <Task pending coro=<ASGIWebsocketConnection.handle_websocket() running at C:\Users\Smart\AppData\Local\Programs\Python\Python37-32\lib\site-packages\quart\asgi.py:135> cb=[_wait.<locals>._on_completion() at C:\Users\Smart\AppData\Local\Programs\Python\Python37-32\lib\asyncio\tasks.py:440]> got Future <Future pending> attached to a different loop

如何将事件循环传递给quart?我尝试获取循环并设置内部函数,但仍然出现错误

loop = asyncio.get_event_loop()
@app.websocket('/wa2tg2')
async def wa2tg2():
while True:
asyncio.set_event_loop(loop)
data = await websocket.receive()
await t2.create_contact()

最佳答案

当你这样做时:

TelegramClient(sn, api_id, api_hash)

电视马拉松需要 asyncio.get_event_loop() 。此方法返回当前线程的事件循环,在您的代码中,该线程是主线程。然后 Telethon 会记住并使用主线程中的循环。

当你这样做时:

Thread(target=tele.client2.run_until_disconnected).start()

您正在创建一个新线程,并且收到相应的错误“任务将 Future 附加到不同的循环”,原因我已经解释过。

使用asyncio时,通常不应该使用线程,除非您确实知道自己在做什么。

事实上,代码可以重写为(删除所有不必要的导入,这些导入可能是在没有经过太多考虑的情况下添加的):

Tele.py

from telethon import TelegramClient, functions, types

client = TelegramClient(sn, api_id, api_hash).start()

async def create_contact():
return await client2(functions.contacts.ImportContactsRequest([
types.InputPhoneContact(0, phone_number, first_name, last_name)
]))

app.py

from quart import Quart, websocket

app = Quart(__name__)

@app.websocket('/wa2tg2')
async def wa2tg2():
while True:
data = await websocket.receive()
await tele.create_contact()

一些注意事项:

  • client.start()无需 await 即可工作。
  • client.run_until_disconnected()仅在需要时才需要调用。
  • async def 通过使用 await 来调用,而不是通过创建单独的线程。
  • 您在输入联系人中使用的 0 只能使用一次(或根本不起作用),因为它需要是一个随机数。
  • 切勿盲目复制粘贴。首先了解它的作用。

关于python - 运行时错误: Task got Future <Future pending> attached to a different loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56689198/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com