在子流程process.terminate()被调用

时间:2019-02-23 09:57:38

标签: python selenium tkinter

我在"first.py"中有此代码

if __name__ == '__main__':
try:
    driver = webdriver.Firefox(executable_path="geckodriver.exe")
    DRIVER = driver
    main(driver)
except Exception as error:
    print(colored(error, "red"))
    print(colored("\nFATAL", "red"))
    time.sleep(20)
finally:
    file = open("quit.txt", "w")
    file.write("quitted")
    file.close()
    driver.quit()

我想通过另一个程序运行"first.py",假设"run_first.py", 所以我在"run_first.py"中写了这个

import tkinter as tk
import subprocess

def Run(): # run first.py
    process = subprocess.Popen(["python.exe", 'first.py'], shell=False,
              stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
              close_fds=True) # start a new process
process_list.append(process) # append the new process in the process_list

def Quit(): # quit first.py
    if len(process_list) > 0:
        for process in process_list: # read all the process in the

process_list并关闭所有它们                 process.terminate()#终止当前进程

root = tk.Tk()

process_list = []
run_button = tk.Button(root , text="Run", command=lambda: Run()) # run button
run_button.grid(column=0, row=1, columnspan=2)

quit_button = tk.Button(root , text="Quit", command=lambda: Quit()) #quit button
quit_button.grid(column=0, row=2, columnspan=2)
root.mainloop()

当我单击“运行”按钮时,它可以正常工作,当我单击“退出”按钮时,问题就出现了,因为程序停止了,但是它不执行finally块,因此未创建文件"quit.txt",并且driver未关闭。 我也尝试使用atexit模块将其放在"first.py"的开头:

import atexit
def finally_do_this():
    file = open("quit.txt", "w")
    file.write("quitted")
    file.close()

atexit.register(finally_do_this)

但这也不起作用

我也试图从父进程中杀死它,但是如果我不作为参数传递它,我将无法由父进程退出webdriver。

我想在父级中创建webdriver并将其作为参数传递给first.py,以便能够在第二时刻杀死它:

def Run():
    driver = webdriver.Firefox(executable_path="geckodriver.exe") # create the webdriver
    # pass it as argoument
    process = subprocess.Popen(["python.exe", 'instagram.py', driver], shell=False,
              stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
              close_fds=True)
    process_list.append(process)

但是python拒绝将webdriver element作为argoument传递

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\bo\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:/Users/bo/Desktop/first/run_bot.py", line 23, in <lambda>
run_button = tk.Button(root , text="Run", command=lambda: Run())
File "C:/Users/bo/Desktop/first/run_bot.py", line 12, in Run
close_fds=True)
File "C:\Users\bo\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 775, in __init__
restore_signals, start_new_session)
File "C:\Users\bo\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 1119, in _execute_child
args = list2cmdline(args)
File "C:\Users\bo\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 530, in list2cmdline
needquote = (" " in arg) or ("\t" in arg) or not arg
TypeError: argument of type 'WebDriver' is not iterable

我正在Windows 10和python 3.7上工作

0 个答案:

没有答案