0

Little problem with multiprocessing used inside Python3 application.

I want to create an application for RPI4, that has GPIO management and cooperate's with written in PyQT5 GUI.

To simplify my problem. Problem is that input() statement in Python make child process with GUI to stop.

This is what's works (code inside main file):

import multiprocessing
from PyQt5 import QtWidgets

def processGUI():
    app = QtWidgets.QApplication(sys.argv)
    mwc = MainWindowController()
    mwc.show()
    app.exec_()


if __name__ == '__main__':
    process1 = multiprocessing.Process(target=processGUI)
    process1.start()

    while True:
        pass

    process1.join()

GUI show's up despite main process going into infinite loop.

Now, if I change that loop into something like that:

import multiprocessing
from PyQt5 import QtWidgets

def processGUI():
    app = QtWidgets.QApplication(sys.argv)
    mwc = MainWindowController()
    mwc.show()
    app.exec_()


if __name__ == '__main__':
    process1 = multiprocessing.Process(target=processGUI)
    process1.start()

    while True:
        strInput = input()

        if strInput == "END":
            break

    process1.join()

GUI don't shows up. Or it show up after I type "END" in standard input.

My question is? Why function input() stop's other process from work?

  • 2
    The actual question is, why do you need `input()` if you're using a GUI based framework? Anyway, your question has already been answered here: [input() is blocking the use of processes](https://stackoverflow.com/questions/23081060/input-is-blocking-the-use-of-processes) – musicamante Sep 26 '20 at 19:48
  • I show this example as simple version of my problem. Instead of this, it would be RPi process which handle sensors and send data through Queue – Maciej Leszczyk Sep 26 '20 at 19:51
  • @musicamante Link you have send me, contains only topic about using input() with threads. Assume that I use input() in main process. – Maciej Leszczyk Sep 26 '20 at 19:56
  • Have you read the first sentence in the accepted answer? "When you call input, it is blocking the entire Python process, not just the thread it runs in." – musicamante Sep 26 '20 at 19:59
  • Yes, I read. They say that **entire process** got blocked. That's why I use two processes to handle this, what is mentioned later. – Maciej Leszczyk Sep 26 '20 at 20:02
  • No. It says that it blocks "the entire **python** process". – musicamante Sep 26 '20 at 20:03
  • _The usual way around blocking problems like this is to use processes instead of threads. If you make InputCatcher into a process rather than a thread, then it becomes a separate OS-level process that the OS can schedule independently, and so the syscall will only block that process and not the main one._ – Maciej Leszczyk Sep 26 '20 at 20:10
  • @musicamante I have created **two** processes that have separate memory from themselves. I did entire thing that is mentioned in link that you gave me. But I don't understand how one process can block other one – Maciej Leszczyk Sep 26 '20 at 20:12
  • @Maciej Leszczyk: You are talking about sensors and RPi. Are you really going to use `input()` to read sensor data ? – Maurice Meyer Sep 26 '20 at 20:44
  • @Maurice Meyer : No. input() is just for debug purpose. I don't admit to use it later. – Maciej Leszczyk Sep 26 '20 at 20:58
  • Btw, I had time to do some checking, and your code actually works for me (so, I'd say that my above comments are not completely valid). I mean, it works at least on Linux and an old Windows XP, so it's either an issue with the combination of OS/python you're using, you didn't test it against the plain python exec, or, maybe, something related to the IDE you're using (remember that IDEs are not always reliable with multiprocessing debugging). In any case, why don't you use a separate (not modal) QDialog if you only need that for debugging? – musicamante Sep 26 '20 at 21:34
  • @musicamante I use PyCharm as my IDE. I didn't realised, why input() blocks other process. But anyway, I replaced it with Queue, which was my target in final program. RPi get data and put it into Queue, which is maintained by separated Thread from GUI. And it works fine for me :) – Maciej Leszczyk Sep 28 '20 at 09:38

0 Answers0