Python PyQt5子进程标准

nlejzf6q  于 2023-02-18  发布在  Python
关注(0)|答案(1)|浏览(178)

我有一个主窗口(PyQt 5),启动其他“script.py“作为子进程在QtThread(script.py也有一个窗口PyQt 5).线程在主程序应该发送一些数据while-True-cycle,我的脚本也应该显示我在他的窗口的数据,现在发送线程.
现在就像:
main.py:

class Send(QtCore.QThread):

 def run(self) -> None:
    self.proc = subprocess.Popen(['python3', 'pipeFile.py'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True, encoding='utf-8')

    for i in range(5):
        print('sended')
        self.proc.write('some data')
        time.sleep(1)

script.py

class Check(QtCore.QThread):
    def __init__(self, parent=None):
        QtCore.QThread.__init__(self, parent)
    def run(self) -> None:
        while True:
            print('read')
            print(sys.stdin.read())

我试着在不同的网站,论坛,我读过的文档中搜索信息,但我不理解它。我怎么能做这个IPC与管道?
对不起,我的英语不好,我不擅长学习语言,但我在努力;如果你能纠正我,那就太好了

eufgjt7s

eufgjt7s1#

所以,它是如此简单...我只需要添加flush()来清除缓冲区
我希望我的问题能帮助到
main.py

class Send(QtCore.QThread):
def __init__(self, parent=None):
    QtCore.QThread.__init__(self, parent)
    self.proc = None

def run(self) -> None:
    # time.sleep(1)
    self.proc = subprocess.Popen(['python3', 'pipeFile.py'], shell=False, stdin=subprocess.PIPE, text=True, encoding='utf-8')

    for i in range(5):
        self.proc.stdin.write('lol\n')
        print('sended')
        self.proc.stdin.flush()

script.py

class Check(QtCore.QThread):
def __init__(self, parent=None):
    QtCore.QThread.__init__(self, parent)

def run(self) -> None:
    while True:
        s = sys.stdin.readline()
        if s != '':
            print('read')
            print(s)
            sys.stdin.flush()

相关问题