启动Python进程后,wait()返回-1,errno = 10

时间:2019-04-08 16:08:51

标签: python c wait

我有一个C程序,该程序在线程上启动Python进程。 Python进程使用Selenium并向FTC提出骚扰电话投诉。 posix_spawn用于使C程序和Python进程由于Python中的内存泄漏而分开。我在管理Python进程时遇到问题。

Python进程的wait()返回-1,其中errno设置为10。根据{{​​3}},这是错误。我相信10ECHILDstrerror返回 No child processes

$ ./test.exe
Process started, pid 2730
call_datetime: 2019-04-04 18:07:00
caller_name: PERRYVILLE   MD
caller_number: 4106425608
Wait failed -1, 10

直接通过脚本运行Python会产生预期的0返回代码。

$ python3 ftc.py --caller_name "PERRYVILLE   MD" --caller_number 4106425608 --call_datetime "2019-04-04 18:07:00"
call_datetime: 2019-04-04 18:07:00
caller_name: PERRYVILLE   MD
caller_number: 4106425608
$ echo "$?"
0

我发现了两个类似的问题。第一个位于wait(2) man page。问题中没有足够的信息,因此已关闭。第二个问题在Linux system() returns -1, ERRNO = 10 No child processes。我不认为这个问题适用,因为SIGCHLD没有得到处理。

为什么wait失败并出现ECHILD


这是C程序。它会调用Python脚本,然后退出。

等待代码摘自system() returns -1, errno=10 when logged into Oracle示例。

$ cat test.c
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>
#include <pthread.h>
#include <spawn.h>
#include <sys/types.h>
#include <sys/wait.h>

#define log_error printf
#define log_info printf
extern char **environ;

int main(int argc, char* args[])
{
    char* const cname = "PERRYVILLE   MD";
    char* const cnumber = "4106425608";
    char* const ctime = "2019-04-04 18:07:00";

    char* const arguments[] = {
        "python3",
        "ftc.py",
        "--caller_name", cname,
        "--caller_number", cnumber,
        "--call_datetime", ctime,
        NULL
    };

    pid_t pid;
    int res = posix_spawn(&pid, "/usr/bin/python3", NULL, NULL, arguments, environ);

    if (res != 0) {
        log_error("Process failed %d, %d\n", res, errno);
        goto do_exit;
    } else {
        log_info("Process started, pid %d\n", pid);
    }

    do
    {
        res = waitpid(pid, &res, WUNTRACED | WCONTINUED);
        if (res == -1) {
            log_error("Wait failed %d, %d\n", res, errno);
            goto do_exit;
        }

        if (WIFEXITED(res)) {
            log_info("Process exited, result %d\n", WEXITSTATUS(res));
        //} else if (WIFSIGNALED(res)) {
        //    log_info("Process signaled, result %d\n", WTERMSIG(res));
        } else if (WIFSTOPPED(res)) {
            log_info("Process stopped, result %d\n", WSTOPSIG(res));
        } else if (WIFCONTINUED(res)) {
            log_info("Process continued\n");
        }
    } while (!WIFEXITED(res) /*&& !WIFSIGNALED(res)*/);

do_exit:

    return (int)WEXITSTATUS(res);
}

C代码使用gcc -Wall -D_GNU_SOURCE -g3 -O1 -std=c99 -pthread test.c -o test.exe进行编译。

这是Python脚本的相关部分。它打印脚本参数,然后退出。

$ cat ftc.py
#!/usr/bin/env python3

import time
import sys

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

def get_option(argv, option):

    argc = len(argv)
    for i in range(0, argc-1):
        if (argv[i] == option and i+1 < argc):
            return argv[i+1]

    return None

def main():

    caller_name = get_option(sys.argv, "--caller_name")
    caller_number = get_option(sys.argv, "--caller_number")
    call_datetime = get_option(sys.argv, "--call_datetime")

    if caller_name is None:
        sys.exit("caller_name is not available")
    if caller_number is None:
        sys.exit("caller_number is not available")
    if call_datetime is None:
        sys.exit("call_datetime is not available")

    print(f"call_datetime: {str(call_datetime)}")
    print(f"caller_name: {str(caller_name)}")
    print(f"caller_number: {str(caller_number)}")

    sys.exit(0)

if __name__ == "__main__":
    main()

1 个答案:

答案 0 :(得分:3)

您将在此处用waitpid()的返回值覆盖等待状态:

    res = waitpid(pid, &res, WUNTRACED | WCONTINUED);

对返回值和等待状态使用不同的变量:

    int ret = waitpid(pid, &res, WUNTRACED | WCONTINUED);
    if (ret == -1) {
        log_error("Wait failed %d, %d\n", ret, errno);
        goto do_exit;
    }