python - 属性错误: 'generator' object has no attribute 'connect' Pydle,异步

标签 python python-3.x async-await python-asyncio

尝试将 pydle 与 asyncio 一起使用。几个月前,我的代码运行得很好,现在我无法再运行它了。

@asyncio.coroutine
class MyOwnBot(pydle.Client):
     async def on_connect(self):
        await self.join('#new')

iclient = MyOwnBot('testeee', realname='tester')
loop = asyncio.get_event_loop()
asyncio.ensure_future(iclient.connect('irc.test.net', 6697, tls=True,tls_verify=False), loop=loop)

但我收到此错误:

asyncio.ensure_future(iclient.connect('irc.test.net', 6697, tls=True, tls_verify=False), loop=loop)
AttributeError: 'generator' object has no attribute 'connect'

最佳答案

pydletakes care of the event loop for you 。另外,将整个类标记为协程是行不通的;类不是工作单元,类上的方法才是工作单元。

为了确保跨 python 版本兼容性,该库包含它的 own async handling module :

import pydle

class MyOwnBot(pydle.Client):
    @pydle.coroutine
    def on_connect(self):
        yield self.join('#new')

iclient = MyOwnBot('testeee', realname='tester')
iclient.connect('irc.test.net', 6697, tls=True, tls_verify=False)

Client.connect() 方法启动循环(但如果您需要在其他地方使用相同的循环,则可以传入一个循环)。

关于python - 属性错误: 'generator' object has no attribute 'connect' Pydle,异步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44007989/

相关文章:

python - 其他选项而不是使用 try-except

python - 使用 python 和 DBSCAN 聚类高维数据

python-3.x - Python subprocess.Popen 结果存储在一个变量中

python - 如何转换为 Python 列表理解

python - 导入 pyodbc 时出错

python - 受限Python : get print output when printing from within a function

python-3.x - 使用python3的简单nltk情感分析代码

c# - 异步模式 - 在从方法返回一些值之前等待事件

javascript - 处理 Async Await 以将 Firebase 与 React Native 结合使用

c# - 在 ASP.NET Identity GetRolesAsync 中使用异步等待丢失 HttpContext