Samuel Joseph
Samuel Joseph

Reputation: 43

Python program pauses when server runs

I am making a python program that communicates with a minecraft server and tells it to start, stop, or any other commands. The program reads an email and executes the command given from the email. When the program enters the command to start the server, it freezes the python program until the minecraft server is stopped.

I have tried to have the program open a batch file that starts the server, but then I don't have any way of making the program turn it off, or type in commands, or read the console.

if data[0]+data[1]+data[2]+data[3]+data[4]+data[5] == 'start ':
    comm = data.replace('start ','')
    try:
        mb = int(float(comm)*1024)
        #calls the command to start the server
        call('java -Xmx' + str(mb) + 'M -Xms' + str(mb) + 'M -jar server.jar nogui')
        #program freezes here until server is stopped
    except Exception:
        call('java -Xmx1024M -Xms1024M -jar server.jar nogui')
    print("started server")
elif data == "stop server" or data == "stop":
    call('stop')
elif data[0]+data[1]+data[2]+data[3]+data[4] == 'call ':
    comm = data.replace('call ','')
    call(comm)
    print("called command")
elif data[0]+data[1]+data[2] == 'op ':
    comm = data
    call(comm)
    print("op player")
else:
    print("not a command")
deletemail(mail,i)
print("deleted item")

I expected to see the program continue and respond to emails, but it froze instead. The program continued off normally after the server was stopped. I know this isn't an error, but is there a way to get the python program to continue while the server is running?

Upvotes: 2

Views: 122

Answers (1)

Tesla67
Tesla67

Reputation: 66

Its is better practice to use subprocess to handle this. You can use popen which will allow you to continue to run your script and send in commands. Here is a good answer on using popen that shows how to send in commands.

Upvotes: 1

Related Questions