python、闭合循环和slack客户端
我正在通过slack的python、闭合循环和slack客户端,python,pyqt5,slack-api,Python,Pyqt5,Slack Api,我正在通过slack的RTMAPI收听slack频道,但当我试图停止收听时,我发现 Traceback (most recent call last): File "C:\Python36\lib\site-packages\aiohttp\client_ws.py", line 227, in receive msg = await self._reader.read() File "C:\Python36\lib\site-packages\aiohttp\streams.p
RTMAPI
收听slack频道,但当我试图停止收听时,我发现
Traceback (most recent call last):
File "C:\Python36\lib\site-packages\aiohttp\client_ws.py", line 227, in receive
msg = await self._reader.read()
File "C:\Python36\lib\site-packages\aiohttp\streams.py", line 631, in read
return await super().read()
File "C:\Python36\lib\site-packages\aiohttp\streams.py", line 591, in read
await self._waiter
concurrent.futures._base.CancelledError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python36\lib\site-packages\slack\rtm\client.py", line 372, in _read_messages
message = await self._websocket.receive(timeout=1)
File "C:\Python36\lib\site-packages\aiohttp\client_ws.py", line 227, in receive
msg = await self._reader.read()
File "C:\Python36\lib\site-packages\async_timeout\__init__.py", line 45, in __exit__
self._do_exit(exc_type)
File "c:\Users\she jong shon\.vscode\extensions\ms-python.python-2019.10.41019\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\_pydev_bundle\pydev_monkey.py", line 667, in __call__
ret = self.original_func(*self.args, **self.kwargs)
File "C:\Python36\lib\threading.py", line 884, in _bootstrap
self._bootstrap_inner()
File "C:\Python36\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Python36\lib\site-packages\fbs_runtime\excepthook\__init__.py", line 84, in run_with_except_hook
run_original(*args2, **kwargs2)
File "C:\Python36\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "c:\Users\she jong shon\Desktop\RTC36\src\main\python\main.py", line 303, in start_loop
self.rtm_client.start()
File "C:\Python36\lib\site-packages\slack\rtm\client.py", line 198, in start
return self._event_loop.run_until_complete(future)
File "C:\Python36\lib\asyncio\base_events.py", line 484, in run_until_complete
return future.result()
File "C:\Python36\lib\site-packages\slack\rtm\client.py", line 340, in _connect_and_read
await self._read_messages()
File "C:\Python36\lib\site-packages\slack\rtm\client.py", line 374, in _read_messages
if not self._websocket.closed:
AttributeError: 'NoneType' object has no attribute 'closed'
基本上,我有一个调用stop()
的按钮,它应该停止rtm_客户端
并退出线程。
我是否因为没有正确退出循环而出错
import sys
import threading
import asyncio
from slack import RTMClient
from PyQt5 import QtCore, QtWidgets
from main_UI import Ui_ApplicationWindow
class SlackClient(QtCore.QObject):
textChanged = QtCore.pyqtSignal(str)
terminated = pyqtSignal()
def start(self):
threading.Thread(target=self._start_loop, daemon=True).start()
def _start_loop(self):
RTMClient.on(event="message", callback=self.say_hello)
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
slack_token = "xoxb-...."
self.rtm_client = RTMClient(token=slack_token)
self.rtm_client.start()
def say_hello(self, **payload):
data = payload["data"]
if data:
if "text" in data:
text = data["text"]
self.textChanged.emit(text)
def stop(self):
self.rtm_client.stop()
self.terminated.emit()
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
super(ApplicationWindow, self).__init__()
self.ui = Ui_ApplicationWindow()
self.ui.setupUi(self)
self.client = SlackClient()
# connections
self.ui.pushButton.clicked.connect(self.client.start)
self.ui.stopButton.clicked.connect(self.client.stop)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
myWindow = ApplicationWindow()
myWindow.show()
sys.exit(app.exec_())
我想你的代码太乱了
QThread
创建线程,但您可以使用threading
在QThread
创建的线程内创建另一个线程。您可以创建asyncio
循环,但也可以使用start()
运行它,而不使用asyncio
。谢谢。是的,python世界的新成员。那么你有什么建议?@bbusdriver为什么要使用QThread?,在我的回答中:不要放它,并解释为什么它是不必要的。嗨,eyllanesc,我使用它是因为进度条不是用QObject
@bbusdriver mmmm,…,它闻起来像一个,在这种情况下,您为什么不询问QProgressBar,并提供我要求的MRE(我请求过您)?您不知道如何在我的初始代码中使用QProgressBar并不意味着您应该使用QThread。请使用@username