【问题标题】:print in new terminal window在新的终端窗口中打印
【发布时间】:2014-09-28 14:18:49
【问题描述】:

我编写了一个具有多个线程的脚本,在这个线程中我使用“print()”代码来打印关于该线程的日志活动,但问题是我不想在一个终端窗口中打印所有这些日志。我在 ubuntu 论坛上找到了这段代码,但它似乎不是一种可以在任何操作系统上运行的标准方式,包括 mac、ubuntu、fedora,...... 有什么标准方法可以在多个终端窗口中输出数据,或者有比这段代码更好的 ida 吗?

import subprocess

pid = subprocess.Popen(args=["gnome-terminal", "--command=python test.py"]).pid
print pid

【问题讨论】:

  • 如果有运行终端程序的标准方法,您的终端程序可能会被称为terminal 而不是gnome-terminal

标签: python linux macos terminal


【解决方案1】:

shell(即 Bash)并不真正了解多个窗口。您可以使用 screen 来管理多个 shell 会话。

例如创建一个新的屏幕会话并在里面执行ifconfig

#create a unique name for the screen session (timestamp + random muber)
timestamp_random=my_$(date +%s)_$RANDOM
#create new screen session in detached mode
screen -S "$timestamp_random" -d -m
#stuff (write) command into that screen session + execute (by hitting newline/ENTER)
screen -r "$timestamp_random" -X stuff $'ifconfig\n'

然后您可以列出所有屏幕会话:

screen -list

并连接到每个会话以查看输出:

screen -R [sessionname]

【讨论】:

    【解决方案2】:

    很难从您的问题中理解您的意思,但以下内容可能会有所帮助。在 OSX 上启动一个终端并按 Command-N 以获取第二个终端 - 这样您就打开了 2 个终端。现在点击其中一个并输入:

    tty
    

    它会告诉您与该窗口关联的终端的名称,例如/dev/ttys000.

    然后进入另一个打开的终端窗口并输入:

    echo Hello > /dev/ttys000        # or whatever the other Terminal was called
    

    您应该会看到 echo 命令的输出出现在另一个终端的窗口中,我认为这就是您的问题的意思。

    【讨论】:

    • 有没有办法从代码中做到这一点?我的应用程序是单线程的,但我希望它在一个终端中显示一些内容,而在另一个终端中显示其他内容
    • @RodrigoLaguna 您可以使用std::cout 写入主窗口,使用std::cerr 将输出发送到另一个窗口。然后,在运行程序之前,进入另一个窗口以获取其名称,然后返回主窗口,运行./yourProgram 2> /dev/ttyXXXX。如果您决定稍后将所有输出放在一个窗口中,只需省略 2> ... 部分。
    猜你喜欢
    • 2014-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 2013-10-28
    • 2013-12-25
    • 2013-08-12
    • 1970-01-01
    相关资源
    最近更新 更多