如何将信号处理程序设置为multiprocessing.Process?

时间:2017-11-27 13:28:51

标签: windows python-3.x selenium multiprocessing signals

如何在目标函数中设置信号处理程序?

multiprocessing.Process(GUI中的cur_process)对象将在用户单击开始按钮时创建并启动。

在用户单击停止按钮之后,我需要正常终止进程。(单击停止按钮时将SIGTERM发送到cur_process) 但是,我使用的是Webdriver,所以我想在kill进程之前退出Webdriver 所以我尝试在目标函数(test_main)中设置信号处理程序,但是尽管单击停止按钮,它仍然不处理SIGTERM。

如何在Process目标函数中设置信号处理程序?

import tkinter as tk
from multiprocessing import Process
from tkinter import messagebox
import sys
import signal
import time
import os
from selenium.webdriver.support.wait import WebDriverWait
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support import expected_conditions as EC


def create_driver():
    options = Options()
    if sys.platform == "darwin":
        options.binary_location = '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary'
    #options.add_experimental_option("detach", True)
    if sys.platform == "win32":
        options.binary_location = "C:\\(user_home)\\AppData\\Local\\Google\\Chrome SxS\\Application\\chrome.exe" #location of chrome canary for windows
    options.add_argument('--headless')
    #options.add_argument('--disable-gpu')
    options.add_argument('--reduce-security-for-testing')
    options.add_argument('--allow-insecure-localhost')
    if sys.platform == "win32":
        chromedriver_path = r".\chromedriver"
    else:
        chromedriver_path = "./chromedriver"

    driver = webdriver.Chrome(chromedriver_path, chrome_options=options)
    return driver



class GUI(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.cur_process = None
        self.pack()
        self.create_widget()

    def create_widget(self):
        self.start = tk.Button(self, width=5, padx=10, pady=3)
        self.start["text"] = "Start"
        self.start["command"] = self.start_event
        self.start.grid(row=2, column=2, columnspan=2, padx=4, pady=10)
        self.quit = tk.Button(self, width=5, padx=10, pady=3)
        self.quit["text"] = "Stop"
        self.quit["command"] = self.stop_event
        self.quit.grid(row=3, column=2, columnspan=2, padx=4, pady=10)

    def start_event(self):
        if self.cur_process is None:
            self.cur_process = Process(target=test_main) #target function
            self.cur_process.start()

    def stop_event(self):
        if self.cur_process != None:
            os.kill(self.cur_process.pid, signal.SIGTERM)
#            self.driver.quit()
            self.cur_process = None


def test_main(): #target function
    # set signal handler to SIGTERM
    def k():
        print("set new signal handler")
        try:
            driver.quit()
            # then terminate process
        except NameError:# driver is not defined
            pass # do nothing
        sys.exit() # terminate process
    signal.signal(signal.SIGTERM, k)
    # do something with driver
    driver = create_driver()
def gui():
    root = tk.Tk()
    root.geometry("400x300")
    g = GUI(master=root)
    g.mainloop()


if __name__ == "__main__":
    gui()

0 个答案:

没有答案