使Popen等待第一个命令完成,然后开始下一个 [英] Make Popen wait for first command to finish then start next

查看:171
本文介绍了使Popen等待第一个命令完成,然后开始下一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行一个程序,并给程序一个脚本如此:

  subprocess.Popen X:\\apps\\Nuke6.1v5\\Nuke6.1.exe','-t','X:\\apps\\Scripts\NUKE\\ nukeExternalControl\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\秒完成启动。所以当它启动程序Popen运行下一个命令,当然因为程序没有启动并运行是错误出来。所以我的问题是如何告诉Popen等待第一个应用程序运行THEN执行Popen的下一部分..任何接受者



UPDATE

  import nukeExternalControl.client 

np = subprocess.Popen(['X: \\\apps\\Nuke6.1v5\\Nuke6.1.exe','-t','X:\\apps\\Scripts\NUKE\\\\
ukeExternalControl\ \server.py'])


print启动Nuke服务器

conn = nukeExternalControl.client.NukeConnection()
nuke = conn .nu​​ke

打印执行命令

nuke.root()。knob('first_frame')。setValue(1)
nuke.root knob('last_frame')。setValue(10)

read = nuke.createNode('CheckerBoard2')
textFrame = nuke.createNode('Text')
textShotName = nuke .createNode('Text')
reformat = nuke.createNode('Reformat')
write = nuke.createNode('Write')

解决方案



感谢jdi问题已经浮现!道歉给他,因为他已经把这个问题拖延了我一段时间...非常感谢!



回答:



我需要在 Popen 命令后使用 time.sleep(),因为我的服务器没有等待

解决方案

在浏览了这个nuke模块的自述文件之后,我感觉你可能会对使用它的实际需求感到困惑。



subprocess.Popen(['X:\\apps\\ Nuke6.1v5\\Nuke6.1.exe','-t','X:\\apps\\Scripts\NUKE\\\\
ukeExternalControl\\server.py'])



...这行(我假设你实际上是分配给一个变量,阻塞它或检查它的状态)是用Nuke启动一个基于非gui的服务器所需要的。 Nuke是Python解释器,可以通过 nuke -t 运行python脚本,因此您正在使用它来启动服务器进程。这将阻止,并等待您使用您的客户类



您的问题中似乎缺少的是更多关于您如何确切地尝试运行此服务器/客户端配置的上下文。如果您尝试在同一个脚本中执行这两个部分,那么您需要在执行时启动服务器进程,然后睡眠一秒钟(服务器进程启动很快),然后运行客户端代码连接。



更新



实际上,启动服务器进程有两种方式在readme中非常清楚地概述:

 要启动一个命令服务器,请添加以下行
您的Nuke menu.py:
---------------------------
import nukeExternalControl.server
nukeExternalControl .server.nuke_command_server()
---------------------------

这是你放在你的nuke menu.py文件中的东西,或者用运行的Nuke应用程序手动启动它。您的应用程序现在将运行服务器进程,并允许客户端连接。



如果您不想使用GUI许可证并将其运行到服务器连接,您可以使用命令行 X:\apps\Nuke6.1v5\Nuke6.1.exe -t X:\apps\Scripts\\ \\NUKE\\\
ukeExternalControl\server.py
,它启动基于终端的服务器。没有理由我可以想到,你需要使用子进程启动服务器在你的脚本中,当他们给你一个方法已经启动它。



解决方案



经过与OP的长时间对话,结果是他想做的是我的回答的第一部分建议。他有一个独立的脚本,想使用Nuke的python解释器做一些事情(完全无头,没有Nuke GUI应用程序)。 使用此第三方模块,他希望在作为服务器的子流程中启动脚本nuke终端。然后他将在他的代码中使用客户端类进行通信(他自己托管一个服务器进程,并与其进行循环通信。



解决方案他的问题是,他需要 time.sleep(2)紧接着Popen开始他的 server.py 。等待几秒钟,服务器才能完全启动,客户端才能成功连接。



是的,他现在欠我一杯啤酒。


I'm trying to run a program and feed the program a script as such:

subprocess.Popen(['X:\\apps\\Nuke6.1v5\\Nuke6.1.exe', '-t', 'X:\\apps\\Scripts\NUKE\\nukeExternalControl\\server.py'])

My problem is that it takes the program a few seconds to finish launching. So while its starting up the program Popen runs the next command and of course because the program is not up and running is errors out. So my question is how do I tell Popen to wait for the first application to run THEN execute the next part of Popen.. any takers??

UPDATE

    import nukeExternalControl.client

    np = subprocess.Popen(['X:\\apps\\Nuke6.1v5\\Nuke6.1.exe', '-t', 'X:\\apps\\Scripts\NUKE\\nukeExternalControl\\server.py'])


    print "Starting Nuke Server"

    conn = nukeExternalControl.client.NukeConnection()
    nuke = conn.nuke

    print "execute commands"

    nuke.root().knob('first_frame').setValue(1)
    nuke.root().knob('last_frame').setValue(10)

    read = nuke.createNode('CheckerBoard2')
    textFrame = nuke.createNode('Text')
    textShotName = nuke.createNode('Text')
    reformat = nuke.createNode('Reformat')
    write = nuke.createNode('Write')

SOLUTION

So! Thanks to jdi the problem has been sovled! Props to him as he has stuck this problem out with me for quite some time... thanks so much!

ANSWER:

I needed to use time.sleep() after the Popen command because my server was not waiting for nuke to start before communicating to it.

解决方案

After glancing over the readme for this nuke module, I get the sense that you might be confused about what is actually required to use it.

subprocess.Popen(['X:\\apps\\Nuke6.1v5\\Nuke6.1.exe', '-t', 'X:\\apps\\Scripts\NUKE\\nukeExternalControl\\server.py'])

... This line (which I assume you are actually assigning to a variable and either blocking on it, or checking its status), is what is required to start a non-gui based server with Nuke. Nuke being a python interpreter can run a python script via nuke -t <script.py>, hence you are using it to start your server process. This will block, and wait for you to use your client class to communicate.

What seems to be missing from your question is more context about how you are exactly trying to run this server/client configuration. If you are attempting to do both parts in the same script, then you would need to start the server process as you are doing, then maybe sleep for a second (the server process starts pretty quickly), and then run the client code that makes the connection.

Update

Realistically there are two ways to start your server process, as very plainly outlined in the readme:

To start a command sever whenever Nuke is launched, add the following lines
to your Nuke menu.py:
---------------------------
import nukeExternalControl.server
nukeExternalControl.server.nuke_command_server()
---------------------------

This is something you would put in your nuke menu.py file, or manually start this with a running Nuke application. Your application will now be running a server process and allow clients to connect.

If you dont want to have to use a GUI license and keep it running to server connections, then you use the other method from the command line X:\apps\Nuke6.1v5\Nuke6.1.exe -t X:\apps\Scripts\NUKE\nukeExternalControl\server.py , which starts a terminal-based server. There is NO reason I can think of that you need to be using subprocess to start the server in your script when they give you a method for starting it already.

Solution

After a lengthy conversation with the OP, it turns out that what he wanted to do was what the first part of my answer suggested. He has a standalone script that wants to do something using Nuke's python interpreter (completely headless without the Nuke GUI app). Using this 3rd party module, he wants to start the script in a subprocess that will act as a server to the nuke terminal. He will then proceed in his code to communicate with it using the client class (he is self hosting a server process and sorta round-robin communicating with it.

The solution to his problem was that he needed to time.sleep(2) right after the Popen that starts his server.py. Waiting a few seconds for the server to completely start allowed the client to successfully connect.

And yes, he owes me a beer now.

这篇关于使Popen等待第一个命令完成,然后开始下一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆