python – Error with Pipe() in multiprocessing

Question:

Main.py

from Test import *
if __name__=="__main__":
    import multiprocessing
    p_out, p_in = multiprocessing.Pipe()
    p1 = multiprocessing.Process(target=test1, args=(p_in,))
    p2 = multiprocessing.Process(target=test2, args=(p_out,))
    p2.start()
    p1.start()

    def test1bind():
        print("F pressed")
        global p1
        if p1.is_alive():
            p1.terminate()
        else:
            p1 = multiprocessing.Process(target=test1, args=(p_in,))
            p1.start()

    def test2bind():
        print("G pressed")
        global p2
        p2.terminate()
        p2 = multiprocessing.Process(target=test2, args=(p_out,))
        p2.start()

    import keyboard
    keyboard.add_hotkey("F", test1bind)
    keyboard.add_hotkey("G", test2bind)

Test.py

from time import sleep

def test1(p_in):
    test = 0
    while True:
        sleep(0.012)
        test = test + 1
        p_in.send(test)

def test2(p_out):
    while True:
        img = p_out.recv()
        print(img)

I get an error if I press F first and then G:

F pressed
G pressed

Process returned 0 (0x0)        execution time : 0.875 s
Для продолжения нажмите любую клавишу . . . Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\andre\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\spawn.py", line 102, in spawn_main
    source_process = _winapi.OpenProcess(
OSError: [WinError 87] Параметр задан неверно

But if I press G first and then F, I get a different error:

F pressed

Process returned 0 (0x0)        execution time : 1.058 s
Process Process-3:
Traceback (most recent call last):
  File "C:\Users\andre\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\connection.py", line 312, in _recv_bytes
    nread, err = ov.GetOverlappedResult(True)
BrokenPipeError: [WinError 109] Канал был закрыт

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\andre\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\process.py", line 315, in _bootstrap
    self.run()
  File "C:\Users\andre\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "E:\1NAIM\NAIM\HELP\Test.py", line 12, in test2
    img = p_out.recv()
  File "C:\Users\andre\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\connection.py", line 250, in recv
    buf = self._recv_bytes()
  File "C:\Users\andre\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\connection.py", line 321, in _recv_bytes
    raise EOFError
EOFError

I don't understand why she appears. I can close many times for example p1, and there will be no errors, as soon as I close them, in any order, an error occurs.

Answer:

I made a "Crutch", but still waiting for a better answer:

Main.py

from Test import *
if __name__=="__main__":
    import multiprocessing
    p_out, p_in = multiprocessing.Pipe()
    p1 = multiprocessing.Process(target=test1, args=(p_in,))
    p2 = multiprocessing.Process(target=test2, args=(p_out,))
    p3 = multiprocessing.Process(target=menu)
    p2.start()
    p1.start()
    p3.start()
    def test1bind():
        print("F pressed")
        global p1
        global p2
        if p1.is_alive():
            p1.terminate()
            p2.terminate()
        else:
            p1 = multiprocessing.Process(target=test1, args=(p_in,))
            p2 = multiprocessing.Process(target=test2, args=(p_out,))
            p1.start()
            p2.start()

    def test2bind():
        print("G pressed")
        global p1
        global p2
        p1.terminate()
        p2.terminate()
        p1 = multiprocessing.Process(target=test1, args=(p_in,))
        p2 = multiprocessing.Process(target=test2, args=(p_out,))
        p1.start()
        p2.start()

    import keyboard
    keyboard.add_hotkey("F", test1bind)
    keyboard.add_hotkey("G", test2bind)

Test.py

from time import sleep

def test1(p_in):
    test = 0
    while True:
        sleep(0.1)
        test = test + 1
        p_in.send(test)

def test2(p_out):
    while True:
        img = p_out.recv()
        print(img)

def menu():
    sleep(99999)

ps needs a background process, otherwise the program will close.

Scroll to Top