Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从GUI程序连续写入串行链接:需要使用线程吗?_Python_Multithreading_Pyqt4 - Fatal编程技术网

Python 从GUI程序连续写入串行链接:需要使用线程吗?

Python 从GUI程序连续写入串行链接:需要使用线程吗?,python,multithreading,pyqt4,Python,Multithreading,Pyqt4,我用PyQt4编写了一个GUI程序,它必须通过串行数据链路发送消息字符串 我用两个按钮小部件实现了一个GUI界面。我需要通过串行链接连续发送数据,单击第一个按钮时每秒发送一次,然后单击第二个按钮时停止 我当前的程序只能在点击按钮的瞬间发送数据。这是我编写的处理按钮单击信号的方法: def sendMessage(self): while 1: print "Hello........" if checke == False: bre

我用PyQt4编写了一个GUI程序,它必须通过串行数据链路发送消息字符串

我用两个按钮小部件实现了一个GUI界面。我需要通过串行链接连续发送数据,单击第一个按钮时每秒发送一次,然后单击第二个按钮时停止

我当前的程序只能在点击按钮的瞬间发送数据。这是我编写的处理按钮单击信号的方法:

def sendMessage(self):
    while 1:
        print "Hello........"
        if checke == False:
            break

是否需要使用线程来解决此问题?

是。GUI编程的关键是永远不要在主线程上执行任何长操作,因为在该操作完成之前,它会阻塞整个程序

如果您想通过网络连续发送数据,请在后台线程中执行

为您提供一些示例代码

class MessageWorker(QtCore.QThread):

    def __init__(self):
        super(ParserWorker, self).__init__()
        self.ok_to_send = False
        self.terminated = True

    def run(self):
        while not self.terminated:
            if self.ok_to_send:
                self.send_message()
            time.sleep(1)

     def start_send():
         self.ok_to_send = True

     def pause_send():
         self.ok_to_send = False

     def terminated():
         self.terminated = False
然后在主程序中只需调用

worker = MessageWorker()
worker.start() # Start the background thread
worker.start_send() # Start sending message
worker.pause_send() # Pause sending message
worker.terminated() # Stop sending message permanently

是的,您需要使用线程。在任何基于GUI的程序中,任何需要花费大量时间的工作都应该在单独的线程上进行,以避免在看到“无响应”程序时阻塞UI,这几乎总是由于程序无法处理窗口消息,因为它的UI线程在某个长操作中被阻塞

启动后台线程的一种简单方法是使用。以下是如何使用它每秒向串行端口写入一次数据:

class MyClass:
    # This method will run on a separate thread
    def _serial_port_worker(self):
        while self._run_worker:
            self.send_data_to_serial_port()
            time.sleep(1)

    # Call this to start the worker thread
    def start_worker_thread(self):
        self._run_worker = True
        worker_thread = threading.Thread(target=self._serial_port_worker,
                                         args=(self,))
        worker_thread.start()

    # Call this to tell the worker thread to stop
    def stop_worker_thread(self):
        self._run_worker = False

这取决于。。。如果发送操作很快,则可以使用该类。它与Qt事件循环集成,因此您不必担心线程问题。串行通信可能会很慢,这取决于您发送的数据量,因此我无法确定这是否是适合您的解决方案。

基本上您有三种选择:

  • 使用第二个线程执行串行通信。GUI工具包并不总是线程安全的,因此您应该只从主线程调用它们。此外,Python中的线程还有一个限制;只能执行Python字节码
  • 使用GUI工具包的超时函数(可能会以不同的方式调用)偶尔创建一个事件。在事件处理程序中执行串行通信。确保使用非阻塞读写(在中,在
    串行
    对象中配置超时),否则您的应用程序可能会失去响应
  • 使用模块从第二个程序进行串行通信。即使第二个进程阻塞,也不会影响GUI。您可以使用在QUI和其他进程之间进行通信

有一些库(如twisted)可以帮助实现这类功能。tooHi,twisted是一种网络编程,不是吗?。我可以将其用于非套接字编程吗?谢谢。您好,我是线程编程新手,您能告诉我如何轻松理解(结合PyQt GUI编程)吗。非常感谢。