Why does subprocess timeout (or wait) not work?

57 views Asked by At

time.sleep() and popen.wait() don't work

Hi, I'm trying to write a programme that would restart the minecraft server after a certain time, I'm trying to do it by writing stop to the console, but I have the following problem: output commands such as MCser.stdin.write(b'say Restart after 30s\n') (code below) are executed simultaneously, ignoring time.sleep(), please help me to fix this.

import subprocess 
from subprocess import Popen, PIPE

MCser = subprocess.Popen(["java.exe", "-Xms8024M", "-Xmx10096M", "-jar", "quilt-server-launch.jar"], stdout = PIPE, stdin = PIPE, stderr = PIPE)
MCser.stdin.write(b'say test\n')
MCser.stdin.write(b'say Restart after 30s\n')
time.sleep(20)
MCser.stdin.write(b'say Restart after 10s\n',)
time.sleep(10)
MCser.stdin.write(b'stop\n')

I also tried using Popen.wait, it also did not give any result, all commands are executed at the same time (code below)

import subprocess 
from subprocess import Popen
from subprocess import PIPE
from subprocess import TimeoutExpired

Zephyr = subprocess.Popen(["java.exe", "-Xms8024M", "-Xmx10096M", "-jar", "quilt-server-launch.jar"], stdout = PIPE, stdin = PIPE, stderr = PIPE)

try:
    Zephyr.wait(timeout=30)
except TimeoutExpired:
    Zephyr.stdin.write(b'say test\n')
    Zephyr.stdin.write(b'say Restart after 30s\n')

try:
    Zephyr.wait(timeout=20)
except TimeoutExpired:
    Zephyr.stdin.write(b'say Restart after 10s\n',)

try:
    Zephyr.wait(timeout=10)
except TimeoutExpired:
    Zephyr.stdin.write(b'stop\n')

Flush and others memory cleaning features also doesn't work

0

There are 0 answers