1

I have tried the following two ways to do something in my project, I used threading first and it kind of worked but when I tried to do it using multiprocessing it just didn't.

The portions of code shown below corresponds to a fuction defined inside init block of X Class.

This is the code done with threading:

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

This is the code done with threads:

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

Basically, when multiprocessing is used, it doesn't set the text of the labels or change the Enabled state of the lineedits. ¿how can I achieve this?

Sorry if I bother you with my ignorance, but please all help will be useful TIA.

1 Answers1

1

This is the expected behavior.

Threads operate in the same memory space; processes have their owns. If you start a new process, it can not make changes in the memory of its parent process. The only way to communicate with a process is IPC, basically network or Unix sockets.

UPD:

Also, you can pause and restart threads, e.g. by using synchronization primitives (locks and semaphores) and checking them from thread function. There is also a less nice way which I really don't recommend. So, I would rather stick to synchronization primitives.

Speaking of the IPC, it is much more troublesome and expensive than synchronizing threads. It is built around sockets, so communicating with a process on the same machine is almost as troublesome as talking to another machine on the other side of the world. Fortunately there are quite a few protocols and libraries providing abstraction over sockets and making it less tedious (dbus is a good example).

Finally, if you really like the idea of decentralized processing, it might make sense to look into message queues and workers. This is basically the same as IPC, but abstracted to a higher level. E.g. you can run a processes to queue tasks on one machine, do processing on another and then get results back into the original program (or yet another machine/process). A popular example here could be something like AMPQ, RabbitMQ or Celery.

Community
  • 1
  • 1
Marat
  • 15,215
  • 2
  • 39
  • 48
  • Thank you Marat, I suppose I have to work on those threads... the problem with them was that I can't pause one to continue with another... but I'll ask another question for that. Thank you again, and if you can, please provide some Information that you consider useful about IPC. – Ronald Hudson Mar 21 '17 at 03:03