我有一个python脚本,必须为dir中的每个文件启动一个shell命令:
import os
files = os.listdir(".")
for f in files:
os.execlp("myscript", "myscript", f)
Run Code Online (Sandbox Code Playgroud)
这适用于第一个文件,但在"myscript"命令结束后,执行停止并且不会返回到python脚本.
我能怎么做?我fork()
以前需要calling os.execlp()
吗?
我可能看起来不够仔细,但我正在尝试用另一个 python 脚本替换正在运行的 python 脚本。根据我所做的研究,使用 os.execl 函数可能就是我正在寻找的。我对该函数应该使用的参数有点困惑。谁能帮我解释一下如何用另一个脚本替换当前运行的 python 脚本。
python中的os.execl()和os.execv()有区别吗?我正在使用
os.execl(python, python, *sys.argv)
Run Code Online (Sandbox Code Playgroud)
重新启动我的脚本(从这里)。但它似乎是从上一个脚本离开的地方开始的。
我希望脚本在重新启动时从头开始。这会不会
os.execv(__file__,sys.argv)
Run Code Online (Sandbox Code Playgroud)
做这份工作吗?来自这里的命令和想法。我从 python 帮助/文档中找不到它们之间的区别。有没有办法干净重启?
有关我正在尝试做的事情的更多背景信息,请参阅我的其他问题
我在使用virtualenv在passenger_wsgi模块上部署Django时遇到了一些麻烦.passenger_wsgi.py文件中的Python代码应该解决我的问题:
import os, sys
INTERP = '/home/login/.virtualenvs/env_name/bin/python'
if sys.executable != INTERP:
os.execl(INTERP, INTERP, *sys.argv)
Run Code Online (Sandbox Code Playgroud)
我理解的前三行,但我对第四行只有一个含糊不清的想法,那就是碰巧给我一个错误:
/home/login/.virtualenvs/env_name/bin/python: can't find '__main__.py' in ''
那么os.execl到底在做什么呢?那个错误信息意味着什么?
您不必从头开始完成完整的代码.问题出在main里面的execl(..)语句中.代码是 -
#include <cstdio>
#include <iostream>
#include <cstring>
#include <unistd.h>
#include <sys/wait.h>
#include <vector>
#define li long int
using namespace std;
char TypedCommandInTerminal[1001];
vector <string> ValidCommands,TypedCommand;
void ShowTerminal()
{
cout<<"User:$ ";
gets(TypedCommandInTerminal);
}
void PushCommands()
{
ValidCommands.push_back("mkdir");
}
void GetCommandIntoVector()
{
TypedCommand.clear();
char *p = strtok(TypedCommandInTerminal," ");
while(p)
{
TypedCommand.push_back(p);
p = strtok(NULL," ");
}
}
bool MatchCommand(string Command)
{
li i;
for(i=0;i<ValidCommands.size();i++)
{
if(ValidCommands[i].compare(Command)==0)
{
return true;
}
}
return false;
}
int main()
{
int status;
string StoredCommand; …
Run Code Online (Sandbox Code Playgroud) 我有一个c ++应用程序包含队列中的某些项目,然后这些项目将由python脚本处理.我想要它,以便最多运行10个python脚本实例.我计划使用execl()来启动python进程,有没有办法告诉进程已经退出而不必将消息传递回父进程?
如果我使用 execl 是可能需要时间完成的子进程,我什么时候需要使用 waitpid?
当我在父级中使用 waitpid 时,它让我的孩子运行,因为 waitpid 的返回值为 0。一段时间后,我试图在另一个函数中使用 waitpid,它用 ECHILD 返回 -1。如果我不确定天气孩子是否完成,我应该什么时候使用 waitpid?
//pid_t Checksum_pid = fork();
Checksum_pid = fork();
if (Checksum_pid == 0)
{
execl(path, name, argument as needed, ,NULL);
exit(EXIT_SUCCESS);
}
else if (Checksum_pid > 0)
{
pid_t returnValue = waitpid(Checksum_pid, &childStatus, WNOHANG);
if ( returnValue > 0)
{
if (WIFEXITED(childStatus))
{
printf("Exit Code: _ WEXITSTATUS(childStatus)") ;
}
}
else if ( returnValue == 0)
{
//Send positive response with routine status running (0x03)
printf("Child process …
Run Code Online (Sandbox Code Playgroud)