- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何在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/
我正在尝试发送一封带有附件和 html 内容的邮件。我知道如何分别发送 html 内容和附件,但是是否可以同时发送 html 和附件? 这是我尝试过的: public static void send
所以我真的很难弄清楚什么时候应该附加到一个对象,什么时候不应该附加到一个对象。首先,这是我的(非常简化的)对象模型的小图。 在我的 DAL 中,我每次执行与数据相关的操作时都会创建一个新的 DataC
更新:Docker 0.9.0 现在使用 libcontainer,从 LXC 转移参见:Attaching process to Docker libcontainer container 我正在运
我按照此页面上的说明进行操作: https://developers.facebook.com/docs/plugins/share-button/#settings 我得到一个工作共享对话框,但是当
我有一个现有代码可以正确下载和处理一些电子邮件。 要处理的电子邮件必须有一个或多个 xml 作为附件,现在我正在迁移这个过程从当前的标准邮件帐户到一个认证系统,该系统将邮件包装到一个新的电子邮件。 因
我不太明白通过 attach api 连接到另一个虚拟机是什么意思.我读到每个 Java 程序都在其自己的虚拟机中运行(参见 here )。那么对于一个程序“附加”到另一个 jvm 进程以便它可以访问
无论如何我可以强制使用这种方法,ActiveStorage::Attached#attach不排队后台工作?换句话说,我想禁用似乎包含在 ActiveStorage::Attached#attach
在 Eclipse 插件开发中,我通过 MANIFEST.MF 文件导入包。所以我没有 lib 文件夹,也没有引用的库部分。 即使对于像 String.format() 这样最基本的方法,我也看不到
我不知道执行此操作的确切方法。我想要一种方法,而不是针对 Eclipse 中所有项目的单个项目。请告诉我如何解决这个问题。 最佳答案 这是在 Eclipse 中的类路径中的一个 jar,你有 附加了一
我有一个多个文件要附加到选择器 View 中。当用户选择该选择器 View 项目时,他们可以单击电子邮件按钮来附加所选文件。我该如何在选择器 View 中执行此操作? 这是我的示例代码。 M 文件:
经过this之后通过讨论,我相信附加到同一虚拟机的选项默认情况下已在 OpenJDK11 中禁用。 我正在尝试将 java 代理升级到 OpenJDK11,在测试用例中,当调用 VirtualMach
首先 - 我知道 UWSGI 建议使用 smart-attach-daemon 来自:http://uwsgi-docs.readthedocs.io/en/latest/AttachingDaemo
我使用axios从Reaction网站调用我的API(Django服务器),我希望在授权头的每个请求中都出现一个带有持有者令牌的授权头。。这就是我如何设置授权头apiClient.defaults.h
我在 heroku 上有一个 rails 应用程序,我无法运行我最新的数据库更改。运行 heroku run rake db:migrate给我 Running `rake db:migrate` a
我使用 strope.js 构建一个简单的 IM(web)。 我有 2 个页面:index.html(用于登录)和 myChat.html(主聊天 View )。 当我通过index.html中的ji
我尝试过“heroku run python manage.py migrate”并收到“超时等待dyno,请参阅https://devcenter.heroku.com/articles/one-o
我正在使用 OpenGL 帧缓冲区对象 (FBO) 在 iOS 上实现模板阴影。代码有效——也就是说,从视觉上看,模板缓冲区正在完成这项工作,而且性能似乎还不错。 但是,当我通过 OpenGL ES
我正在尝试使用 slack 附件来记录应用程序错误,但是像堆栈跟踪这样的大字段表现得很奇怪。 首先,当使用附件时,表格似乎固定为任意宽度,是否有任何更改,以便可以允许更宽的值?否则 50+% 的松弛窗
我花了好几天时间寻找一种解决方案,将带有附件的属性字符串放到 NSPasteboard 上。 我可以读取带有附件的 RTFD 文件,修改其文本和属性,然后将其粘贴到 NSPasteboard 上以供其
我想对数据框的列进行许多修改。但是,由于需要大量的列和转换,我想避免一遍又一遍地使用数据框名称。 在 SAS 数据步中,在一个数据步中,您可以创建一个变量并在定义后立即引用它: data A; s
我是一名优秀的程序员,十分优秀!