我不知道需要多线程,工作为主,或异步,但基本上我有一个PowerShell脚本函数,它的几个参数是否调用这个,我需要使用不同的参数调用了几次,有这些运行在平行下.
目前,我调用这样的函数:
Execute "param1" "param2" "param3" "param4"
Run Code Online (Sandbox Code Playgroud)
如何在不等待每次调用Execute返回调用者的情况下多次调用它?
目前我正在运行v2.0,但如有必要,我可以更新
编辑:这是我到目前为止,这是行不通的:
$cmd = {
param($vmxFilePath,$machineName,$username,$password,$scriptTpath,$scriptFile,$uacDismissScript,$snapshotName)
Execute $vmxFilePath $machineName $username $password $scriptTpath $scriptFile $uacDismissScript $snapshotName
}
Start-Job -ScriptBlock $cmd -ArgumentList $vmxFilePath, $machineName, $username $password, $scriptTpath, $scriptFile, $uacDismissScript, $snapshotName
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
无法将'system.object []'转换为参数'initializationscript'所需的'system.management.automation.scriptblock'类型.不支持指定的方法
编辑2:我修改了我的脚本,但我仍然得到上面提到的错误.这是我的mod:
$cmd = {
param($vmxFilePath,$machineName,$username,$password,$scriptTpath,$scriptFile,$uacDismissScript,$snapshotName)
Execute $vmxFilePath $machineName $username $password $scriptTpath $scriptFile $uacDismissScript $snapshotName
}
Start-Job -ScriptBlock $cmd -ArgumentList $vmxFilePath, $machineName, $username $password, $scriptTpath, $scriptFile, $uacDismissScript, $snapshotName
Run Code Online (Sandbox Code Playgroud) 在我的程序中,我在一个有限的while循环中分支(并行)子进程,并在每个进程上执行exec.我希望父进程在所有子进程终止后才恢复执行(此while循环后的点).我该怎么办?
我尝试了几种方法.在一种方法中,我在while循环之后使父进行暂停,并且仅当waitpid返回错误ECHILD(没有剩余子进程)时才从SIGCHLD处理程序发送一些条件但是我在这种方法中遇到的问题甚至在父进程完成所有进程之前,retStat变为-1
void sigchld_handler(int signo) {
pid_t pid;
while((pid= waitpid(-1,NULL,WNOHANG)) > 0);
if(errno == ECHILD) {
retStat = -1;
}
}
**//parent process code**
retStat = 1;
while(some condition) {
do fork(and exec);
}
while(retStat > 0)
pause();
//This is the point where I want execution to resumed only when all children have finished
Run Code Online (Sandbox Code Playgroud)