python、closing循环和slack clien

2024-05-15 11:11:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在通过slack的RTM API收听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_client并退出线程。 我是否因为没有正确退出循环而出错?你知道吗

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_()) 

Tags: inpyselfclientloopreadlibpackages