【问题标题】:What is the difference between these threading and multiprocessing codes in python 2.x?python 2.x中的这些线程和多处理代码有什么区别?
【发布时间】:2017-03-20 23:31:11
【问题描述】:

我在我的项目中尝试了以下两种方法来做某事,我首先使用线程并且它有点工作但是当我尝试使用多处理来做它时它没有。

下面显示的部分代码对应于在 X Class 的 init 块中定义的函数。

这是使用线程完成的代码:

def Exec_Manual():
    while True:
        for i in range(0,5):
            if self.rbtnMan.isChecked():
                if self.rbtnAuto.isChecked():#This is another radio button.
                    break
                self._tx_freq1_line_edit.setEnabled(1)
                self._tx_freq2_line_edit.setEnabled(1)
                self._tx_freq3_line_edit.setEnabled(1)
                self._tx_freq4_line_edit.setEnabled(1)
                self._tx_freq5_line_edit.setEnabled(1)
                frec = 'self._tx_freq'+str(i+1)+'_line_edit.text()'
                efrec = float(eval(frec))
                self.lblTx1.setText(str(efrec-0.4))
                self.lblTx2.setText(str(efrec))
                self.lblTx3.setText(str(efrec+0.4))
                #print frec
                print efrec
                time.sleep(1)

manual_thread = threading.Thread(target=Exec_Manual)
manual_thread.daemon = True
manual_thread.start()

这是使用线程完成的代码:

def Exec_Manual():
    while True:
        for i in range(0,5):
            if self.rbtnMan.isChecked():
                if self.rbtnAuto.isChecked():
                    break
                self._tx_freq1_line_edit.setEnabled(1)
                self._tx_freq2_line_edit.setEnabled(1)
                self._tx_freq3_line_edit.setEnabled(1)
                self._tx_freq4_line_edit.setEnabled(1)
                self._tx_freq5_line_edit.setEnabled(1)
                frec = 'self._tx_freq'+str(i+1)+'_line_edit.text()'
                efrec = float(eval(frec))
                self.lblTx1.setText(str(efrec-0.4))
                self.lblTx2.setText(str(efrec))
                self.lblTx3.setText(str(efrec+0.4))
                #print frec
                print efrec
                time.sleep(1)

proceso_manual = multiprocessing.Process(name='txmanual', target=Exec_Manual)
proceso_manual.daemon = True
proceso_manual.start()

基本上,当使用多处理时,它不会设置标签的文本或更改行编辑的启用状态。 ¿我怎样才能做到这一点?

抱歉,我的无知打扰了您,但请所有帮助都会对 TIA 有用。

【问题讨论】:

    标签: python python-2.7 python-multithreading python-multiprocessing


    【解决方案1】:

    这是预期的行为。

    线程在同一个内存空间中运行;进程有自己的。如果启动一个新进程,它就不能在其父进程的内存中做出改变。与进程通信的唯一方法是 IPC,基本上是网络或 Unix 套接字。

    更新:

    此外,您可以暂停和重新启动线程,例如通过使用同步原语(锁和信号量)并从线程函数中检查它们。还有一种我真的不推荐的less nice 方式。所以,我宁愿坚持同步原语。

    说起IPC,它比同步线程麻烦多了,成本也高多了。它是围绕套接字构建的,因此与同一台机器上的进程通信几乎与与世界另一端的另一台机器通信一样麻烦。幸运的是,有相当多的协议和库提供对套接字的抽象并使其不那么繁琐(dbus 就是一个很好的例子)。

    最后,如果您真的喜欢去中心化处理的想法,那么研究一下消息队列和工作线程可能是有意义的。这与IPC基本相同,但抽象到更高的层次。例如。您可以运行进程以在一台机器上对任务进行排队,在另一台机器上进行处理,然后将结果返回到原始程序(或另一台机器/进程)。这里的一个流行示例可能是 AMPQ、RabbitMQ 或 Celery。

    【讨论】:

    • 谢谢 Marat,我想我必须处理这些线程...问题是我无法暂停一个线程以继续另一个线程...但我会问另一个问题为了那个原因。再次感谢您,如果可以的话,请提供一些您认为对 IPC 有用的信息。
    猜你喜欢
    • 2012-09-27
    • 1970-01-01
    • 2011-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-28
    • 2016-04-20
    • 2020-01-04
    相关资源
    最近更新 更多