简体   繁体   English

让Popen等待第一个命令完成,然后再开始

[英]Make Popen wait for first command to finish then start next

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. 因此,Popen在启动程序时会运行下一条命令,当然,这是因为该程序未启动并且正在运行会导致错误。 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?? 所以我的问题是我如何告诉Popen等待第一个应用程序运行然后执行Popen的下一部分。

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! 多亏了jdi,这个问题才得以解决! 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. 我需要在Popen命令之后使用time.sleep() ,因为我的服务器在与之通信之前没有等待nuke的启动。

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. 浏览了此nuke模块的自述文件后,我觉得您可能对使用它的实际要求感到困惑。

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启动基于非GUI的服务器所需要的。 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. 作为Python解释器的Nuke可以通过nuke -t <script.py>运行python脚本,因此您正在使用它来启动服务器进程。 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. 您可以将其放入您的nuke menu.py文件中,或者使用正在运行的Nuke应用程序手动启动它。 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. 如果您不想使用GUI许可证并保持运行到服务器连接,则可以从命令行 X:\\apps\\Nuke6.1v5\\Nuke6.1.exe -t X:\\apps\\Scripts\\NUKE\\nukeExternalControl\\server.py使用其他方法X:\\apps\\Nuke6.1v5\\Nuke6.1.exe -t X:\\apps\\Scripts\\NUKE\\nukeExternalControl\\server.py ,它将启动基于终端的服务器。 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. 经过与OP的漫长交谈之后,事实证明他想做的就是我的回答的第一部分。 He has a standalone script that wants to do something using Nuke's python interpreter (completely headless without the Nuke GUI app). 他有一个独立的脚本,希望使用Nuke的python解释器(无需Nuke GUI应用程序就完全没有头)来执行某些操作。 Using this 3rd party module , he wants to start the script in a subprocess that will act as a server to the nuke terminal. 他希望使用第3方模块在一个子进程中启动脚本,该子进程将充当nuke终端的服务器。 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 . 解决他的问题的方法是,在启动他的server.py的Popen之后,他需要time.sleep(2) Waiting a few seconds for the server to completely start allowed the client to successfully connect. 等待几秒钟,以使服务器完全启动,客户端才能成功连接。

And yes, he owes me a beer now. 是的,他现在欠我一杯啤酒。

If the thing you're reffering to when you say "next command/thing" is 'X:\\\\apps\\\\Scripts\\NUKE\\\\nukeExternalControl\\\\server.py' , then you're quite confused. 如果说“ next command / thing”时要引用的内容是'X:\\\\apps\\\\Scripts\\NUKE\\\\nukeExternalControl\\\\server.py' ,那么您就很困惑。 That's an command line argument passed to nuke.exe (the same way as C:\\ is passed when you execute dir C:\\ ). 这是传递给nuke.exe命令行参数 (与执行dir C:\\时传递C:\\方式相同)。

There's simply no way to control what nuke.exe does from the python side... you have to check in your server.py that the exe is finished "booting". 根本没有办法从python端控制nuke.exe功能……您必须在server.py中签入该exe文件已完成“启动”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM