当我添加 UDP 代码时,pyqtgraph 和 python 冻结

Posted

技术标签:

【中文标题】当我添加 UDP 代码时,pyqtgraph 和 python 冻结【英文标题】:pyqtgraph and python freeze when I add in UDP code 【发布时间】:2021-09-28 18:41:53 【问题描述】:

我正在尝试实时记录一些 EEG 数据,并且它需要在某些事件开始时接收命令。但是,当我从 UDP 添加 receive data 行时,它会捕获并停止工作。 可以发送数据

这里是pygtgraph代码

class Graph:
    def __init__(self, board_shim):
        self.board_id = board_shim.get_board_id()
        self.board_shim = board_shim
        self.exg_channels = BoardShim.get_exg_channels(self.board_id)
        self.sampling_rate = BoardShim.get_sampling_rate(self.board_id)
        self.update_speed_ms = 50
        self.window_size = 8
        self.num_points = self.window_size * self.sampling_rate

        self.app = QtGui.QApplication([])
        self.win = pg.GraphicsWindow(title='BrainFlow Plot',size=(800, 600))

        self._init_timeseries()

        timer = QtCore.QTimer()
        timer.timeout.connect(self.update)
        timer.start(self.update_speed_ms)
        QtGui.QApplication.instance().exec_()

    def _init_timeseries(self):
        self.plots = list()
        self.curves = list()
        
        for i in range(len(self.exg_channels)+1):
            p = self.win.addPlot(row=i,col=0)
            p.showAxis('left', False)
            p.setMenuEnabled('left', False)
            p.showAxis('bottom', False)
            p.setMenuEnabled('bottom', False)
            if i == 0:
                p.setTitle('TimeSeries Plot')
            self.plots.append(p)
            curve = p.plot()
            self.curves.append(curve)

def update(self):  
        received_data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
        print("Received message: ", received_data)  

        data = self.board_shim.get_current_board_data(self.num_points)

        if data[0:7, 0:250].shape == (7, 250):
            for count, channel in enumerate(self.exg_channels):
                # plot timeseries
                DataFilter.perform_lowpass(data[channel], BoardShim.get_sampling_rate(args.board_id), high, 3,
                                            FilterTypes.BUTTERWORTH.value, 0)
                DataFilter.perform_highpass(data[channel], BoardShim.get_sampling_rate(args.board_id), low, 3,
                                            FilterTypes.BUTTERWORTH.value, 0)
                DataFilter.perform_bandstop(data[channel], BoardShim.get_sampling_rate(args.board_id), 50, 2, 8,
                                            FilterTypes.BUTTERWORTH.value, 0)
                self.curves[count+1].setData(data[channel][-1001:].tolist())

            window = normalise(data[0:8, -250:]) # input window
            window = window - window[3, :] # Cz reference

            x = normalise(window)
            x = np.vstack((x[0:3, :], x[4:, :]))

            x = create_data(x.T, fs, 1, low, high, n_freqs, zeros, length) # convert to PSD
            x = np.reshape(x, (1, n_channels_ref, n_freqs))
            x_csp = csp2.transform(x)

            window = np.reshape(window, (1, window.shape[0], window.shape[1]))
            x_raw_csp = csp1.transform(window)

            inference = np.hstack((x_csp, x_raw_csp))

            current_time = datetime.now()
            current_time = current_time.strftime("%M:%S")
            result.append(model.predict(inference)[0])

            MESSAGE = str(model.predict(inference)[0])
            MESSAGE = bytes(MESSAGE, 'utf-8')
            sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
            pygame.time.delay(100)

            self.curves[0].setData(result[-1001:])

        self.app.processEvents()

如果我删除

received_data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print("Received message: ", received_data) 

然后一切正常,但如果我添加它,那么 python 会冻结。我已经在其他东西上测试了这段代码,它可以工作。我还看到其他人将 UDP 与 pyqtgraph 一起使用,所以我知道这是可能的。

【问题讨论】:

请提供minimal reproducible example 【参考方案1】:

默认情况下,sockets 处于阻塞模式。 recvfrom 将停止执行,直到数据发送到套接字。您可以使用 setblocking 更改默认套接字行为。

【讨论】:

感谢您的评论,但是当我将阻止设置为 False 时出现BlockingIOError: [Errno 35] Resource temporarily unavailable 错误

以上是关于当我添加 UDP 代码时,pyqtgraph 和 python 冻结的主要内容,如果未能解决你的问题,请参考以下文章

使用 pyqtgraph 添加新数据栏的最快方法

pyQtgraph 绘图延迟串行绘图

pyqtgraph - ImportError:没有名为 pyqtgraph 的模块

重新启动实时绘图时,pyqtgraph实时绘图中断

pyqtgraph:为散点图添加图例项

无法使用新数据点更新 Pyqtgraph 图