【问题标题】:How to Run Python Script from Remote Machine via Powershell Asynchronously?如何通过 Powershell 异步从远程机器运行 Python 脚本?
【发布时间】:2018-11-21 03:03:30
【问题描述】:

我有一个只能同步工作的 powershell 函数。我想更新此函数,以便可以并行触发对 Python 脚本的多个调用。有没有人知道如何使用 powershell 异步调用 Python 脚本,记住每个 Python 脚本本质上都是一个 while 循环,直到 24 小时过去才会结束。 powershell 函数接受远程机器、python 虚拟环境、python 脚本的路径和参数。

这是目前为止所做的:

function Run-Remote($computerName, $pname, $scriptPath, $pargs)
{
    $cred = Get-QRemote-Credential
    Invoke-Command -ComputerName $computerName -Credential $cred -ScriptBlock {powershell -c "$Using:pname '$Using:scriptPath' $Using:pargs"} -ConfigurationName QRemoteConfiguration
}

【问题讨论】:

    标签: python function powershell parallel-processing asynchronous


    【解决方案1】:

    非常简单的修复,只需在调用命令中添加“-AsJob”即可。这将使 powershell 为您发送的每个命令启动一个新进程。然后在脚本末尾使用“Get-Job | Wait-Job”等待所有进程完成。和“Get-Job | Receive-Job”来取回任何数据。

    我建议阅读有关 powershell 作业的信息,它们非常有用但不直观。

    function Run-Remote($computerName, $pname, $scriptPath, $pargs)
    {
        $cred = Get-QRemote-Credential
        Invoke-Command -AsJob -ComputerName $computerName -Credential $cred -ScriptBlock {powershell -c "$Using:pname '$Using:scriptPath' $Using:pargs"} -ConfigurationName QRemoteConfiguration
    }
    
    Get-Job | Wait-Job
    

    【讨论】:

    • 太棒了!非常感谢。我试试看。
    • 我尝试使用AsJob参数,但它没有在任务机中生成python进程。
    • 这很奇怪,作业完成了吗?我唯一能想到的是脚本在作业完成之前退出
    • 它没有。我改用 -indisconnectedsession 。你预见到使用它会有什么问题吗?
    猜你喜欢
    • 1970-01-01
    • 2022-11-25
    • 2017-03-14
    • 2015-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-14
    • 1970-01-01
    相关资源
    最近更新 更多