如何在单独的窗口中打印多个线程的输出?

问题描述 投票:0回答:1

我正在编写一个代码,我可以使用Raspberry Pi从不同的传感器获得输入。有什么方法可以在不同的控制台窗口中获得输出。

代码是这些方面的东西。

def usensor():
    distance1=GPIO.input(port1)
    print("Distance Left: %s" %distance1)
    distance=GPIO.input(port)
    print("Distance Right: %s" %distance)

def lsensor():
    distance=Linput();                          #predefined function
    print("Forward Distance: %s" %distance)

def main():
    a = threading.Thread(target=usensor)
    s = threading.Thread(target=lsensor)
    a.start()
    s.start()

if __name__ == '__main__':
    main()

如何在单独的窗口中显示两个线程的输出,以便更容易阅读?

python multithreading
1个回答
0
投票

没有进入编程窗口(这在Python中非常实用,但可能没有复杂性),我建议潜行并使用named pipe

假设您创建了一个名为/tmp/pipe1的命名管道(它们通常称为FIFO)。您可以让程序打开此FIFO以进行输出,并将其中一个输出流写入此(调查file函数的print参数)。

您需要做的就是打开另一个终端窗口,然后运行命令

cat /tmp/pipe1

当您的程序写入命名管道时,输出应出现在第二个窗口中。瞧!

© www.soinside.com 2019 - 2024. All rights reserved.