Resource icon

Jieguan Human Simulation Runtime Scheduler [Beta] v3

VERSION 3 CHANGES UNTESTED
CHANGELOG:

  • Fix logic issues
    • The check for "should program end completely" happened at the beginning of second while loop so it would still sleep for random time before exiting - I added an exit at bottom so that it tells you when it is done and exits :)
      Code:
      if ((time.perf_counter()-starttime)/hours) >= int(timedefault):[do exit stuff]

    • The zhipei error did not have a pause so no one could see zhipei is a noob
Python:
import time
import random
import sys
import os
import subprocess
import PySimpleGUI as sg
from datetime import datetime


form = sg.FlexForm('PythonTest')
layout = [
          [sg.Text('Please enter your runtime settings')],
          [sg.Text('How long to run (hours):', size=25), sg.InputText('12', key='runtime')],
          [sg.Text('Interval of running (hours):', size=25), sg.InputText('2', key='minrangehours'), sg.InputText('5',key='maxrangehours')],
          [sg.Text('Interval of break (minutes):', size=25), sg.InputText('15', key='minrangeminutes'), sg.InputText('60', key='maxrangeminutes')],
          [sg.Submit(), sg.Cancel()]
         ]
window = sg.Window('Run Schedule', layout)   
event, values = window.read()
if event == 'Cancel':
    os._exit(0)
window.close()   


try: #Make sure values are integers!
    for item in values:
        int(values[item])
except ValueError as e:
    print ('You are noob like zhipei! Only number work in program') #how dare u call me noob zhipei (╬◥益◤)
    os.system("pause")
    os._exit(0)
starttime=time.perf_counter()
timedefault=values['runtime']
minrun = int(values['minrangehours'])
maxrun = int(values['maxrangehours'])
minbreak = int(values['minrangeminutes'])
maxbreak = int(values['maxrangeminutes'])
hours=int(3600)
minutes=int(60)
print(datetime.now().strftime("%H:%M:%S - ")+ "Running for " + str(timedefault) + " hours")

while( ((time.perf_counter()-starttime)/hours) < int(timedefault)):
    runtime = random.randint(minrun*hours, maxrun*hours)
    print(datetime.now().strftime("%H:%M:%S - ") + "This run will be for " + str(round(runtime/hours, 2)) + " hours")
    startrun=time.perf_counter()
    arr = os.listdir()
    filename = ""
    for file in arr:
        if file.endswith(".exe"):
            if len(file)==16:
                filename = file
    if filename == "":
        print("FILE NOT FOUND")
        os._exit(0)
    DETACHED_PROCESS = 0x00000008
    pid = subprocess.Popen([filename],creationflags=DETACHED_PROCESS).pid
    while( ((time.perf_counter()-startrun) < runtime) and (((time.perf_counter()-starttime)/hours) < int(timedefault)) ):
        pass

    sleeptime = random.randint(minbreak*minutes,maxbreak*minutes)
    temppath = os.getenv("LocalAppData")
    arr = os.listdir(temppath + "\\Temp")
    runningfile = ""
    for file in arr:
        if file.endswith(".exe"):
            if len(file)==16:
                runningfile = file
    subprocess.call("taskkill /im "+runningfile, stdout=subprocess.PIPE)
    subprocess.call("taskkill /im "+ "d2r.exe", stdout=subprocess.PIPE)
    if ((time.perf_counter()-starttime)/hours) >= int(timedefault):
        print(datetime.now().strftime("%H:%M:%S - ") + "Finished running. Stopping.")
        os.system("pause")
        os._exit(0)
        
    print(datetime.now().strftime("%H:%M:%S - ") + "taking break for " + str(round(sleeptime/minutes, 2)) + " minutes")
    time.sleep(sleeptime)
changelog: added timestamp (in 24 hour format)


v2 source codewith timestamps:
Code:
import time
import random
import sys
import os
import subprocess
import PySimpleGUI as sg
from datetime import datetime


form = sg.FlexForm('PythonTest')
layout = [
          [sg.Text('Please enter your runtime settings')],
          [sg.Text('How long to run (hours):', size=25), sg.InputText('12', key='runtime')],
          [sg.Text('Interval of running (hours):', size=25), sg.InputText('2', key='minrangehours'), sg.InputText('5',key='maxrangehours')],
          [sg.Text('Interval of break (minutes):', size=25), sg.InputText('15', key='minrangeminutes'), sg.InputText('60', key='maxrangeminutes')],
          [sg.Submit(), sg.Cancel()]
         ]
window = sg.Window('Run Schedule', layout)   
event, values = window.read()
if event == 'Cancel':
    os._exit(0)
window.close()   


try: #Make sure values are integers!
    for item in values:
        int(values[item])
except ValueError as e:
    print ('You are noob like zhipei! Only number work in program') #how dare u call me noob zhipei (╬◥益◤)
starttime=time.perf_counter()
timedefault=values['runtime']
minrun = int(values['minrangehours'])
maxrun = int(values['maxrangehours'])
minbreak = int(values['minrangeminutes'])
maxbreak = int(values['maxrangeminutes'])
hours=int(3600)
minutes=int(60)
print(datetime.now().strftime("%H:%M:%S - ")+ "Running for " + str(timedefault) + " hours")

while( ((time.perf_counter()-starttime)/hours) < int(timedefault)):
    runtime = random.randint(minrun*hours, maxrun*hours)
    print(datetime.now().strftime("%H:%M:%S - ") + "This run will be for " + str(round(runtime/hours, 2)) + " hours")
    startrun=time.perf_counter()
    arr = os.listdir()
    filename = ""
    for file in arr:
        if file.endswith(".exe"):
            if len(file)==16:
                filename = file
    if filename == "":
        print("FILE NOT FOUND")
        sys.exit()
    DETACHED_PROCESS = 0x00000008
    pid = subprocess.Popen([filename],creationflags=DETACHED_PROCESS).pid
    while( ((time.perf_counter()-startrun) < runtime) and (((time.perf_counter()-starttime)/hours) < int(timedefault)) ):
        pass

    sleeptime = random.randint(minbreak*minutes,maxbreak*minutes)
    print(datetime.now().strftime("%H:%M:%S - ") + "taking break for " + str(round(sleeptime/minutes, 2)) + " minutes")
    temppath = os.getenv("LocalAppData")
    arr = os.listdir(temppath + "\\Temp")
    runningfile = ""
    for file in arr:
        if file.endswith(".exe"):
            if len(file)==16:
                runningfile = file
    subprocess.call("taskkill /im "+runningfile, stdout=subprocess.PIPE)
    subprocess.call("taskkill /im "+ "d2r.exe", stdout=subprocess.PIPE)
    time.sleep(sleeptime)
Top