[英]wait() returns -1, errno=10 after launching Python process
我有一個C程序可以在線程上啟動Python進程。 Python進程使用Selenium並向FTC提出騷擾電話投訴。 posix_spawn
用於將C程序和Python進程由於Python中的內存泄漏而分開。 我在管理Python流程時遇到問題。
Python進程的wait()
返回-1
, errno
設置為10
。 根據wait(2)
手冊頁,這是一個錯誤。 我相信10
是ECHILD
。 strerror
返回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
我發現了兩個類似的問題。 第一個是在Linux system()返回-1,ERRNO = 10沒有子進程 。 問題中沒有足夠的信息,因此已關閉。 第二個問題是在登錄到Oracle時 , system()返回-1,errno = 10 。 我不認為這個問題適用,因為未處理SIGCHLD
。
為什么ECHILD
wait
失敗?
這是C程序。 它調用Python腳本,然后退出。
等待代碼取自wait(2)
手冊頁示例。
$ 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()
您將在此處用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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.