我想使用 fork 和 execv 從 C 程式呼叫 Python 腳本作為子行程。我已經測驗過它呼叫 bin/ls 并且它有效:
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
int status;
char *args[2];
args[0] = "/bin/ls";
args[1] = NULL;
if ( fork() == 0 ){
printf("I am the child\n");
execv( args[0], args ); }
else {
printf("I am the parent\n");
wait( &status ); }
return 0;
}
所以我修改了 C 代碼來呼叫這個 Python 檔案中的 Create_Buffer() 函式:
#!/usr/bin/python3
from nltk.corpus import gutenberg
def Create_Buffer():
out_buf = []
pointer = id(out_buf)
value_in = 21
print("I was here")
print(value_in)
return pointer
def NLTK_Python_Libs(n):
if n == 0:
n = n 1
return id(n)
Python 腳本有兩個功能;我想呼叫 Create_Buffer(),所以我將我的 C 檔案更改為:
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
int status;
char * paramsList[] = { "/bin/bash", "-c", "/usr/bin/python", "/opt/P01_SH/NLTK_Gutenberg/NLTK_Python_Libs.Create_Buffer()", (char *)NULL };
if ( fork() == 0 ){
printf("I am the child\n");
execv("/usr/bin/python",paramsList); }
else {
printf("I am the parent\n");
wait( &status ); }
return 0;
}
但是當我運行它時出現語法錯誤:
我是家長
我是孩子
檔案“字串”,第 1 行
/usr/bin/python
語法錯誤:無效語法
接下來我稍微改變了它,但我得到了一個不同的錯誤(如下所述):
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
int status;
char *args[5];
args[0] = "/bin/bash";
args[1] = "-c";
args[2] = "/usr/bin/python";
args[3] = "/opt/P01_SH/NLTK_Gutenberg/NLTK_Python_Libs.Create_Buffer()";
args[4] = NULL;
if ( fork() == 0 ){
printf("I am the child\n");
execv(args[0], args); }
else {
printf("I am the parent\n");
wait( &status ); }
return 0;
}
這一次它呼叫了 Python 命令列而不是呼叫程式:
我是家長
我是孩子
Python 3.8.0(默認,2021 年 2 月 25 日,22:10:10)
[GCC 8.4.0] 在 Linux 上
輸入“幫助”、“著作權”、“信用”或“許可”以獲取更多資訊。
[Python 命令提示符]
The errors seem to implicate the shebang line at the top of the Python file, but I get the same errors whether I use #!/usr/bin/python, #!/usr/bin/python3 or #!/usr/bin/env python3, or leave the shebang line out.
So my question is: how do I specify the arguments for fork-execv when I want to call a program in a Python .py file? I'm open to using any of the other exec* family, except that I want to use the absolute path, as above, rather than adding to Python PATH, so I don't want to use exec calls with "p" in the name.
I have researched extensively but I haven't found any examples that show the syntax to call a specific program within a Python file and pass arguments to the program. I have seen examples calling a Python file without specifying a program within the file and without passing arguments, but I need to call the function by name.
Compile the C program with:
gcc -fPIC -ggdb -o NLTK_Python.exe NLTK_Python.c -Wall -Wextra
I'm using Python 3.8 on Ubuntu 18.04.
Thanks for any help with this.
uj5u.com熱心網友回復:
我認為這就是你想要的:
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
int status;
char * paramsList[] = {
"/usr/bin/python",
"-m",
"NLTK_Python_Libs",
"-c",
"NLTK_Python_Libs.Create_Buffer()",
(char *)NULL };
if ( fork() == 0 ){
printf("I am the child\n");
execv(paramsList[0],paramsList); }
else {
printf("I am the parent\n");
wait( &status ); }
return 0;
}
該-m選項告訴它加載NLTK_Python_Libs模塊。然后-c告訴它從該模塊執行一個函式。您不能將腳本路徑名與函式名結合起來以您所做的方式執行。
根本沒必要用/bin/bash。python直接執行就好了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/345151.html
標籤:python c python-3.x linux exec
