Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

python - 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\ScriptsNUKE\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\ScriptsNUKE\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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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\ScriptsNUKE\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:appsNuke6.1v5Nuke6.1.exe -t X:appsScriptsNUKE ukeExternalControlserver.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...