如何在不同的窗口中打印多线程的输出?

2024-09-27 07:31:54 发布

您现在位置:Python中文网/ 问答频道 /正文

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

代码就是这样的。在

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

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


Tags: 代码targetinputgpiomaindefthreadstart
1条回答
网友
1楼 · 发布于 2024-09-27 07:31:54

如果不进入编程窗口(在Python中非常实用,但可能没有复杂性),我建议您偷偷地使用named pipe。在

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

您只需打开另一个终端窗口,然后运行命令

cat /tmp/pipe1

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

相关问题 更多 >

    热门问题