Gurpreet Sachdeva
Gurpreet Sachdeva

Reputation: 318

Windows python script to run a server and continue

I am working on my python script to launch a server, may be in background or in a different process and then further do some processing before killing the launched server.

Once the rest of the processing is over, then kill the launched server.

For Example

server_cmd = 'launch_server.exe -source '+ inputfile
print server_cmd
cmd_pid = subprocess.Popen(server_cmd).pid
...
...
... #Continue doing some processing
cmd_pid.terminate() # Once the processing is done, terminate the server

Some how the script does not continue after launching the server as the server may be running in infinite loop listening for a request. Is there a good way to send this process in background so that it doesn't expect for command line input.

I am using Python 2.7.8

Upvotes: 2

Views: 240

Answers (2)

Gurpreet Sachdeva
Gurpreet Sachdeva

Reputation: 318

Making a small change resolved the problem

server_proc = subprocess.Popen(server_cmd, stdout=subprocess.PIPE)

server_proc.terminate()

Thanks Xu for correction in terminate.

Upvotes: 2

Logan Ding
Logan Ding

Reputation: 1771

It's odd that your script does not continue after launching the server command. In subprocess module, Popen starts another child process while the parent process (your script) should move on.

However in your code there's already a bug: cmd_pid is an int object and does not have terminate method. You should use subprocess.Popen object to call terminate method.

Upvotes: 2

Related Questions