简体   繁体   中英

Python multiprocess leads to restarting of script

I have a function logout which results in a timeout and blocks my script (due to server conditions after the logout on the server)

To work around this I tried calling the logout function from another process

def action(br, act):
    '''
        Push submit button on html page opened in browser, act = value of the button
        WARNING: The form is the first one!
    '''
    br.select_form(nr=0)
    br.submit(name=act, label=act)
def login(br):
    for x in range(0,5):
        action(br, 'ENT')
def logout(br):
    set_menu_root(br)
    action(br, 'ESC')

def safe_logout(br):
    try:
        proc = multiprocessing.Process(target=logout, args=(br))
        proc.start()
        time.sleep(1)
        proc.terminate()
    except:
        pass
def reboot(br):
    safe_logout(br)
    login(br)

But due some reason, it first continues the infinite loop as excepted, until the raw input, then the script suddenly restarts

print 'script start'
while True:
    set_menu_root(br)
    print 'Telegram test'
    menu = raw_input('set paramters of menu: ')
    if not go_to_menu(br, menu):
        print 'Menu not found'
        continue
    if len(menu) > 2:
        menu = menu[:-1]

    for n,x in enumerate(id_array):
        if x.value == menu:
            index = n
            break
    action(br, 'ENT')        
    para = get_para(br, menu_ends[index])
    for n,x in enumerate(para):
        print str(n) + ') ' + x

    i = int(raw_input('Select the value u want to map to parameter ' ) )
    set_para(br, menu_ends[index], para[i])
    raw_input("Check")
    reboot(br)

Putting my script code behin an

if __name__ == '__main__'

Did the trick

Credit to @tdihp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM