我有一些 GPU 测试软件,我正在尝试使用 python3 自动化,测试通常会运行 3 分钟,然后由用户使用 ctrl+c 取消,生成以下输出
使用 ctrl+c 退出后,可以再次运行测试,没有问题
当尝试使用子进程 popen 并发送 SIGINT 或 SIGTERM 来自动执行此操作时,我得到的结果与使用键盘输入时的结果不同。脚本突然退出,并且在后续运行中找不到 GPU(假设它没有正确卸载驱动程序)
from subprocess import Popen, PIPE
from signal import SIGINT
from time import time
def check_subproc_alive(subproc):
return subproc.poll() is None
def print_subproc(subproc, timer=True):
start_time = time()
while check_subproc_alive(subproc):
line = subproc.stdout.readline().decode('utf-8')
print(line, end="")
if timer and (time() - start_time) > 10:
break
subproc = Popen(['./gpu_test.sh', '-t', '1'], stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=False)
print_subproc(subproc)
subproc.send_signal(SIGINT)
print_subproc(subproc, False)
Run Code Online (Sandbox Code Playgroud)
如何将 ctrl+c 发送到子进程,就像用户键入它一样?
**更新
import subprocess …Run Code Online (Sandbox Code Playgroud) 使用此示例结构:
typedef struct
{
uint8_t ary[2];
uint32_t val;
}test_t;
Run Code Online (Sandbox Code Playgroud)
这两个代码片段在功能上是等效的:
Snip 1(括号内的箭头操作):
int main()
{
printf("Hello World");
printf("\n %d", sizeof(((test_t *)0)->ary));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Snip 2(括号外的箭头操作):
int main()
{
printf("Hello World");
printf("\n %d", sizeof((test_t *)0)->ary);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果 ary 的大小发生变化,两者都会报告正确的值,但是我不明白外部括号示例如何工作:
sizeof((test_t *)0) // Using memory address 0 as pointer to test_t. size of pointer should be 4 on 32 bit system
Run Code Online (Sandbox Code Playgroud)
因此,snip 2 中的外部箭头操作应等效于:
4->ary or *4.ary
Run Code Online (Sandbox Code Playgroud)
那么为什么 snip 2 的编译和运行与 snip 1 相同
Expected output: 2
Run Code Online (Sandbox Code Playgroud)