Python多进程导致重新启动脚本

时间:2015-06-16 09:44:20

标签: python multiprocessing python-multiprocessing

我有一个函数注销导致超时并阻止我的脚本(由于服务器注销后的服务器条件)

要解决这个问题,我尝试从另一个进程调用logout函数

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)

但由于某种原因,它首先继续无限循环为例外,直到原始输入,然后脚本突然重启

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)

1 个答案:

答案 0 :(得分:2)

将我的脚本代码放在

if __name__ == '__main__'

诀窍

归功于@tdihp