【问题标题】:incomplete representation of ctrl+c functionality of terminals in python codepython代码中终端的ctrl + c功能的不完整表示
【发布时间】:2021-03-28 03:39:59
【问题描述】:

我有一个应用程序,我通过以下命令在终端中执行:

rosbag record -O /home/user/some_address/file /topic

在上面的地址中做了一个file.bag.active,里面基本上记录了一些信息。

在终端中按 ctrl+c 一段时间后,file.bag.active 将变为 file.bag,并准备好被另一个应用程序读取。

然而,我想通过一个 python 脚本完成这个序列,最好的方法似乎是执行以下操作:

import subprocess, shlex

command = "rosbag record -O /home/user/some_address/file /topic"
command = shlex.split(command)
self.proc = subprocess.Popen(command)

表示 ctrl+c 函数的脚本的另一部分是:

self.proc.send_signal(subprocess.signal.SIGINT)

但是,在这种情况下,file.bag.active 文件不会更改为 file.bag,我不确定我还能做什么或添加到此代码中。

我还注意到,如果我添加一个 pdb.set_trace() 并在调用它时在其中按 ctrl+c,我会得到我要查找的结果,但我无法解释为什么或如何表示这样的结果自动代码中的功能。

有人知道解决方法吗?

【问题讨论】:

  • (顺便说一句,你在这里并不需要shlex();你的命令很简单而且是静态的。self.proc = subprocess.Popen(['rosbag', 'record', '-O', '/home/user/some_address/file', '/topic'])。)
  • @tripleee 是的,这只是我使用的另一个示例的修改副本。我也试过没有它,但结果是一样的。

标签: python subprocess ros popen rosbag


【解决方案1】:

得到答案here,即:

import subprocess, shlex, psutil
command = "rosbag record -O subset /camera/depth/image_raw /camera/rgb/image_raw /joy /mobile_base/sensors/imu_data_raw"
command = shlex.split(command)
rosbag_proc = subprocess.Popen(command)

对于 ctrl+c 功能:

for proc in psutil.process_iter():
    if "record" in proc.name() and set(command[2:]).issubset(proc.cmdline()):
        proc.send_signal(subprocess.signal.SIGINT)

rosbag_proc.send_signal(subprocess.signal.SIGINT)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-27
    • 2022-08-22
    • 1970-01-01
    • 2012-08-09
    相关资源
    最近更新 更多