Example #1
0
    def do_terminate(self):
        assert self.hProcess is not None
        assert self.hThread is not None

        start = default_timer()

        logging.debug("ATTEMPT at termination")
        graceful = True
        if graceful:
            logging.debug("### send Ctrl+C")
            # avoid terminating our process
            win32api.SetConsoleCtrlHandler(None, True)
            win32api.GenerateConsoleCtrlEvent(win32console.CTRL_C_EVENT, self.pid)
        else:
            win32api.TerminateProcess(self.hProcess, 15)
        self.post_terminate()
        logging.debug("POST terminate DONE")

        # If the HandlerRoutine parameter is NULL,  a
        # TRUE value causes the calling process to ignore CTRL+C input, and a
        # FALSE value restores normal processing of CTRL+C input. This attribute of ignoring or processing CTRL+C is inherited by child processes.
        #
        # HAS TO BE called AFTER process has been terminated
        win32api.SetConsoleCtrlHandler(None, False)

        diff = default_timer() - start
        logger.debug("Termination took: %.4f" % diff)

        logging.debug("TERMINATE OVER")
Example #2
0
 def __init__(self, time_to_force_termination: int,
              argument: List[str]) -> None:
     self.argument = argument
     basicConfig(stream=sys.stdout, level=DEBUG)
     super().__init__(time_to_force_termination)
     win32api.SetConsoleCtrlHandler(None, False)
     win32api.SetConsoleCtrlHandler(self.handle, True)
Example #3
0
File: server.py Project: zizai/pydy
    def _stop_server(self, signalnum, frame):
        """
        Confirms and stops the visualization server
        signalnum:
            signal number
        frame:
            None or a frame object

        """
        # restore previous SIGINT handler
        if os.name == 'nt':  # windows
            win32api.SetConsoleCtrlHandler(self._handle, 0)
        else:
            if self._sigint_handler is not None:
                signal.signal(signal.SIGINT, self._sigint_handler)
                self._sigint_handler = None

        res = raw_input("Shutdown this visualization server ([y]/n)? ")
        if not res or res[0].lower() == 'y':
            print("Shutdown confirmed")
            print("Shutting down server...")
            self.httpd.stop()
            self._thread.join()
        else:
            print("Resuming operations...")
            self._register_sigint_handler()
Example #4
0
def run():
    if sys.platform.startswith('win'):
        import win32api
        import win32con
        import _thread

        def _win_handler(event, hook_sigint=_thread.interrupt_main):
            if event == 0:
                hook_sigint()
                return 1
            return 0
        # Add the _win_handler function to the windows console's handler function list
        win32api.SetConsoleCtrlHandler(_win_handler, 1)

    options = docopt(__doc__)
    print(options)

    max_step = train_config['max_step'] if options['--max-step'] == 'None' else int(options['--max-step'])
    max_episode = train_config['max_episode'] if options['--max-episode'] == 'None' else int(options['--max-episode'])
    save_frequency = train_config['save_frequency'] if options['--save-frequency'] == 'None' else int(options['--save-frequency'])
    name = train_config['name'] if options['--name'] == 'None' else options['--name']
    seed = int(options['--seed'])
    share_args, unity_args, gym_args = train_config['share'], train_config['unity'], train_config['gym']

    # gym > unity > unity_env
    run_params = {
        'share_args': share_args,
        'options': options,
        'max_step': max_step,
        'max_episode': max_episode,
        'save_frequency': save_frequency,
        'name': name,
        'seed': seed,
    }
    if options['--gym']:
        trails = int(options['--gym-models'])
        if trails == 1:
            gym_run(default_args=gym_args, **run_params)
        elif trails > 1:
            processes = []
            for i in range(trails):
                p = Process(target=gym_run,
                            args=(
                                gym_args,
                                share_args,
                                options,
                                max_step,
                                max_episode,
                                save_frequency,
                                name + '-' + str(i),
                                seed + i * 10
                            ))
                p.start()
                time.sleep(1)
                processes.append(p)
            [p.join() for p in processes]
        else:
            raise Exception('trials must be greater than 0.')
    else:
        unity_run(default_args=unity_args, **run_params)
    def globalSetUp(self):

        self.setup = True

        if os.name == "nt":
            import win32api
            win32api.SetConsoleCtrlHandler(
                TestJig.on_exit,
                True)  # omogoci izhod tekom testiranja, izklopi vse naprave
        else:
            TestJig.set_exit_handler(
                TestJig.on_exit
            )  # omogoci izhod tekom testiranja, izklopi vse naprave

        TestJig.breme(0)  # preventivni izklop bremena
        TestJig.power_set(0.0)  # preventivni izklop pred zacetkom testiranja
        TestJig.izklopi(1, 2, 4)  # preventivni izklop main napajanja
        TestJig.izklopi(
            1, 2, 5)  # preventivni izklop main reverse polarity napajanja
        TestJig.izklopi(1, 3, 4)  # preventivni izklop service port napajanja
        TestJig.izklopi(
            1, 3,
            5)  # preventivni izklop service port reverse polarity napajanja
        TestJig.connect_battery(
            0)  # preventivni izklop baterijskega simulatorja
Example #6
0
def instart(cls, name, display_name=None, stay_alive=True):
    from os.path import splitext, abspath
    from sys import modules

    cls.__svc_name_ = name
    cls.__svc_display_name_ = display_name
    try:
        module_path = modules[cls.__module__].__file__
    except AttributeError:
        from sys import executable
        module_path = executable
    module_file = splitext(abspath(module_path))
    cls.__svc_reg_class = "%s.%s" % (module_file, cls.__name__)
    if stay_alive: win32api.SetConsoleCtrlHandler(lambda x: True, True)
    try:
        try:
            print "[+] TestModule Started : %s" % (name)
            win32serviceutil.QueryServiceStatus(cls.__name__)
        except:
            win32serviceutil.InstallService(
                cls.__svc_reg_class,
                cls.__svc_name_,
                cls.__svc_display_name_,
                startType=win32service.SERVICE_AUTO_START)
            print "[+] Service Has been installed"
        else:
            try:
                win32serviceutil.StartService(cls._svc_name_)
            except Exception, e:
                print "[!] %s : %s" % (e.__class__, str(e))
            else:
Example #7
0
    def run(self):
        log.info("pid: %s" % os.getpid())

        try:
            self.wait_for_journal()
        except KeyboardInterrupt:
            return

        # TODO: windows specific
        if WINDOWS:
            win32api.SetConsoleCtrlHandler(self.console_ctrl_handler, 1)

        with open(JOURNALFILE, "rb") as fp:
            fp.seek(self.seekpos)
            t = tailer.Tailer(fp)
            for line in t.follow():
                #print "line: %s" % repr(line)

                if self.exit_requested:
                    break

                if line == tailer.Idle:
                    if self.last_committed_txid != self.txid:
                        self.commit(self.txid, fp.tell())
                    time.sleep(1.0)  # TODO: config
                else:
                    self.process_line(fp, line)

        log.info("exiting")
Example #8
0
File: etc.py Project: modsim/molyso
    def correct_windows_signal_handlers():
        """
        Corrects Windows signal handling, otherwise multiprocessing solutions will not correctly
        exit if Ctrl-C is used to interrupt them.

        :return:
        """
        os.environ['PATH'] += os.path.pathsep + os.path.dirname(
            os.path.abspath(sys.executable))

        try:
            # noinspection PyUnresolvedReferences
            import win32api

            def _handler(_, hook=_thread.interrupt_main):
                hook()
                return 1

            win32api.SetConsoleCtrlHandler(_handler, 1)

        except ImportError:
            logger.warning(
                "Running on Windows, but module 'win32api' could not be imported to fix signal handler.\n"
                + "Ctrl-C might break the program ..." +
                "Fix: Install the module!")
Example #9
0
    def install(self):
        """Installs the service so it can be started and stopped (if it's not installed yet).

        Returns:
            True when installing the service was a success and false
            when it failed.
        """

        # Build up wrapper script
        script_name = self.service.__class__.__name__
        module_name = win32serviceutil.GetServiceClassString(self.service.__class__)
        module_name = module_name.replace('.' + script_name, '')
        print(module_name)
        print(script_name)

        # Enable auto-start if needed
        service_start_type = None
        if self.auto_start:
            win32api.SetConsoleCtrlHandler(lambda control_handler: True, True)
            service_start_type = win32service.SERVICE_AUTO_START

        # Install the service
        win32serviceutil.InstallService(
            self.class_name,
            self.name,
            self.name,
            startType = service_start_type,
            description = self.description
        )
Example #10
0
    def set_signal_handler(self, sigs=None):
        """Set the signal handler to manage_signal (defined in this class)

        Only set handlers for:
        - signal.SIGTERM, signal.SIGINT
        - signal.SIGUSR1, signal.SIGUSR2
        - signal.SIGHUP

        :return: None
        """
        if sigs is None:
            sigs = (signal.SIGTERM, signal.SIGINT, signal.SIGUSR1,
                    signal.SIGUSR2, signal.SIGHUP)

        func = self.manage_signal
        if os.name == "nt":  # pragma: no cover, no Windows implementation currently
            try:
                import win32api
                win32api.SetConsoleCtrlHandler(func, True)
            except ImportError:
                version = ".".join([str(i) for i in os.sys.version_info[:2]])
                raise Exception("pywin32 not installed for Python " + version)
        else:
            for sig in sigs:
                signal.signal(sig, func)
Example #11
0
    def start(self,inputCSVFilePath):
        self.succeeded = {}

        def onExit(sig):
            if sig == 2:
                succeeded = self.succeeded
                self.succeeded = {}
                inputCSVFilePathParts = os.path.splitext(inputCSVFilePath)
                outputSucceededCSVFile = open("Succeeded".join(inputCSVFilePathParts),"w",newline="")
                outputSucceededCSVFileWriter = csv.writer(outputSucceededCSVFile)
                outputSucceededCSVFileWriter.writerow(["Search Url","Repeats","Product ASIN"])
                for (searchUrl,productASIN),repeats in succeeded.items():
                    if repeats > 0:
                        outputSucceededCSVFileWriter.writerow([searchUrl,str(repeats),productASIN])
                outputSucceededCSVFile.close()
        win32api.SetConsoleCtrlHandler(onExit,True)

        entries = []
        inputCSVFile = open(inputCSVFilePath)
        inputCSVFileReader = csv.reader(inputCSVFile)
        for row in itertools.islice(inputCSVFileReader,1,None):
            searchUrl,repeats,*productsASINs = row
            repeats = int(repeats)
            for productASIN in productsASINs:
                self.succeeded[(searchUrl,productASIN)] = 0
            for _ in range(repeats):
                entries.append((searchUrl,productsASINs))
        inputCSVFile.close()
        shuffle(entries)
        for searchUrl,productsASINs in entries:
            self.queue.put((self.openProduct,searchUrl,productsASINs))
        self.queue.join()
        onExit(2)
Example #12
0
def run_app(args):
    """Run the client/server application

    Parameters
    ----------
    args
    """
    # avoid one of two error messages from 'forrtl error(200)' when running
    # on windows cmd - seems related to scipy's underlying Fortran
    os.environ['FOR_DISABLE_CONSOLE_CTRL_HANDLER'] = 'T'

    if USE_WIN32:
        # Set handler for CTRL-C. Necessary to avoid `forrtl: error (200):
        # program aborting...` crash on CTRL-C if we're runnging from Windows
        # cmd.exe
        def handler(dw_ctrl_type, hook_sigint=_thread.interrupt_main):
            """Handler for CTRL-C interrupt
            """
            if dw_ctrl_type == 0:  # CTRL-C
                hook_sigint()
                return 1  # don't chain to the next handler
            return 0  # chain to the next handler

        win32api.SetConsoleCtrlHandler(handler, 1)

    # Create backend server process
    _run_server(args)
Example #13
0
def run():
    if sys.platform.startswith('win'):
        import win32api
        import win32con
        import _thread

        def _win_handler(event, hook_sigint=_thread.interrupt_main):
            if event == 0:
                hook_sigint()
                return 1
            return 0
        # Add the _win_handler function to the windows console's handler function list
        win32api.SetConsoleCtrlHandler(_win_handler, 1)

    options = docopt(__doc__)
    print(options)

    max_step = train_config['max_step'] if options['--max-step'] == 'None' else int(options['--max-step'])
    max_episode = train_config['max_episode'] if options['--max-episode'] == 'None' else int(options['--max-episode'])
    save_frequency = train_config['save_frequency'] if options['--save-frequency'] == 'None' else int(options['--save-frequency'])
    name = train_config['name'] if options['--name'] == 'None' else options['--name']
    share_args, unity_args, gym_args = train_config['share'], train_config['unity'], train_config['gym']

    # gym > unity > unity_env
    run_params = {
        'share_args': share_args,
        'options': options,
        'max_step': max_step,
        'max_episode': max_episode,
        'save_frequency': save_frequency,
        'name': name
    }
    gym_run(default_args=gym_args, **run_params) if options['--gym'] else unity_run(default_args=unity_args, **run_params)
Example #14
0
def set_exit_handler(func):
    try:
        import win32api
        win32api.SetConsoleCtrlHandler(func, True)
    except ImportError:
        import signal
        signal.signal(signal.SIGTERM, func)
Example #15
0
def installService(cls, name, display_name=None, stay_alive=True):
    cls._svc_name_ = name
    cls._svc_display_name_ = display_name or name
    try:
        module_path = modules[cls.__module__].__file__
    except AttributeError:
        # maybe py2exe went by
        from sys import executable
        module_path = executable
    module_file = splitext(abspath(module_path))[0]
    cls._svc_reg_class_ = '%s.%s' % (module_file, cls.__name__)
    if stay_alive:
        win32api.SetConsoleCtrlHandler(lambda x: True, True)
    try:
        win32serviceutil.InstallService(
            cls._svc_reg_class_,
            cls._svc_name_,
            cls._svc_display_name_,
            startType=win32service.SERVICE_AUTO_START)
        print 'Installing service %s... done' % name
        win32serviceutil.StartService(cls._svc_name_)
        print 'Starting service %s... done' % name
        print '\nAll done!'

    except Exception, x:
        print str(x)
Example #16
0
 def disable_os_signals(self):
     if sys.platform == "win32":
         import win32api  # ignoring the signal
         win32api.SetConsoleCtrlHandler(None, True)
     else:
         signal.signal(signal.SIGTERM, signal.SIG_IGN)
         signal.signal(signal.SIGINT, signal.SIG_IGN)
Example #17
0
 def enable_os_signals(self):
     if sys.platform == "win32":
         import win32api
         win32api.SetConsoleCtrlHandler(self.os_signal_handler, True)
     else:
         signal.signal(signal.SIGTERM, self.os_signal_handler)
         signal.signal(signal.SIGINT, self.os_signal_handler)
Example #18
0
def instart(cls, name, display_name=None, stay_alive=True):
    '''
    Install and  Start (auto) a Service
    
    cls : the class (derived from Service) that implement the Service
    name : Service name
    display_name : the name displayed in the service manager
    stay_alive : Service will stop on logout if False
    '''
    cls._svc_name_ = name
    cls._svc_display_name_ = display_name or name
    try:
        module_path = modules[cls.__module__].__file__
    except AttributeError:
        # maybe py2exe went by
        from sys import executable
        module_path = executable
    module_file = splitext(abspath(module_path))[0]
    cls._svc_reg_class_ = '%s.%s' % (module_file, cls.__name__)
    if stay_alive:
        win32api.SetConsoleCtrlHandler(lambda x: True, True)
    try:
        win32serviceutil.InstallService(
            cls._svc_reg_class_,
            cls._svc_name_,
            cls._svc_display_name_,
            startType=win32service.SERVICE_AUTO_START)
        print "Install ok"
        win32serviceutil.StartService(cls._svc_name_)
        print 'Start ok'
    except Exception, x:
        print str(x)
Example #19
0
	def run():
		supported_browsers=['chrome','firefox']
		parser=ArgumentParser()
		parser.add_argument('-u','--url',help='Set URL | Set path to URL list',metavar='URL|FILE')
		parser.add_argument('-p','--processes',default=15,type=int,help='Set number of processes',metavar='N')
		parser.add_argument('-B','--browser',choices=supported_browsers,help='Set browser',metavar='BROWSER')
		parser.add_argument('-P','--proxies',help='Set path to proxy list',metavar='FILE')
		parser.add_argument('-R','--referer',help='Set referer | Set path to referer list',metavar='REFERER|FILE')
		parser.add_argument('-U','--user-agent',help='Set user agent | Set path to user agent list',metavar='USER_AGENT|FILE')
		args=parser.parse_args()
		urls=URLs(args.url or Input.get('URL'))
		Program.browser=args.browser or Input.select('Browser',supported_browsers)
		print('For next options click ENTER to use [default value].')
		proxies=Proxies(args.proxies or Input.get('Proxies [proxy list from API]'))
		referers=Referers(args.referer or Input.get('Referers [https://google.com]'))
		user_agents=UserAgents(args.user_agent or Input.get('User agents [random user agent]'))
		executable_path=WebDriver.install_if_not_installed(Program.browser)
		extension_path=Extension.install_if_not_installed(Program.browser)
		Program.processes=[Process(target=Bot.run,args=(urls,Program.browser,proxies,referers,user_agents,executable_path,extension_path),daemon=True) for _ in range(args.processes)]
		print(f'Starting {args.processes} processes...')
		for process in Program.processes:
			process.start()
		print(f'Started {args.processes} processes.')
		if os.name=='nt':
			import win32api
			win32api.SetConsoleCtrlHandler(Program.quit)
		elif os.name=='posix':
			import signal
			signal.signal(signal.SIGINT,Program.quit)
		print(f'Click ctrl+c when you want to stop the bot.')
		for process in Program.processes:
			process.join()
Example #20
0
def _RunChild(runfn):
    del sys.argv[1]  # The --spawn arg.
    # Create a new thread that just waits for the event to be signalled.
    t = threading.Thread(target=_WaitForShutdown,
                         args=(int(sys.argv[1]), )
                         )
    del sys.argv[1]  # The stop handle
    # This child process will be sent a console handler notification as
    # users log off, or as the system shuts down.  We want to ignore these
    # signals as the service parent is responsible for our shutdown.

    def ConsoleHandler(what):
        # We can ignore *everything* - ctrl+c will never be sent as this
        # process is never attached to a console the user can press the
        # key in!
        return True
    win32api.SetConsoleCtrlHandler(ConsoleHandler, True)
    t.setDaemon(True)  # we don't want to wait for this to stop!
    t.start()
    if hasattr(sys, "frozen"):
        # py2exe sets this env vars that may screw our child process - reset
        del os.environ["PYTHONPATH"]

    # Start the buildbot/worker app
    runfn()
    print("Service child process terminating normally.")
Example #21
0
def adjust_interrupt_handlers():
    import os
    import imp
    import ctypes
    import _thread
    import win32api

    # Load the DLL manually to ensure its handler gets
    # set before our handler.
    basepath = imp.find_module('numpy')[1]

    def try_to_load(dll_path):
        try:
            ctypes.CDLL(dll_path)
        except OSError as e:
            pass
    try_to_load(os.path.join(basepath, 'core', 'libmmd.dll'))
    try_to_load(os.path.join(basepath, 'core', 'libifcoremd.dll'))

    # Now set our handler for CTRL_C_EVENT. Other control event
    # types will chain to the next handler.
    def handler(dwCtrlType, hook_sigint=_thread.interrupt_main):
        if dwCtrlType == 0:  # CTRL_C_EVENT
            hook_sigint()
            return 1  # don't chain to the next handler
        return 0  # chain to the next handler

    win32api.SetConsoleCtrlHandler(handler, 1)
Example #22
0
def enable_ctrl_logoff_handler():
    if HAS_WIN32:
        ctrl_logoff_event = 5
        win32api.SetConsoleCtrlHandler(
            lambda event: True if event == ctrl_logoff_event else False,
            1
        )
Example #23
0
def __call_service_handler():
    def __ctrlHandler(ctrlType):
        return True

    from opsbro.windows_service.windows_service import Service
    win32api.SetConsoleCtrlHandler(__ctrlHandler, True)
    win32serviceutil.HandleCommandLine(Service)
Example #24
0
    def __init__(self, config, base='', driver=None):

        self.base_directory = base
        today = datetime.datetime.utcnow()
        if datetime.datetime.now().hour >= 10 and datetime.datetime.now(
        ).hour <= 16:
            today = today + datetime.timedelta(days=1)
        self.night = 'n' + today.strftime('%Y%m%d')

        self.logger_name = 'ASCOM_CAMERA'
        self.logger = utils.setup_logger(self.base_directory, self.night,
                                         self.logger_name)
        self.header_buffer = ''

        # if you don't know what your driver is called, use the ASCOM Chooser
        # this will give you a GUI to select it
        if driver == None:
            x = win32com.client.Dispatch("ASCOM.Utilities.Chooser")
            x.DeviceType = 'Camera'
            driver = x.Choose(None)
            print("The driver is " + driver)

    # initialize the camera
        try:
            self.camera = win32com.client.Dispatch(driver)
        except:
            x = win32com.client.Dispatch("ASCOM.Utilities.Chooser")
            x.DeviceType = 'Camera'
            driver = x.Choose(None)
            print("The driver is " + driver)

        win32api.SetConsoleCtrlHandler(self.safe_close, True)
        atexit.register(self.safe_close, 'signal_argument')
Example #25
0
def unhandle_ctrl(handler):
    if os.name != 'nt':
        signal.signal(signal.SIGINT, handler)
        return 1
    else:
        import win32api
        return win32api.SetConsoleCtrlHandler(handler, 0)
Example #26
0
def handle_ctrl(hook_sigint=None):
    if os.name != 'nt':
        import signal
        original_sigint_handler = signal.getsignal(signal.SIGINT)
        signal.signal(signal.SIGINT, hook_sigint)
        return original_sigint_handler
    else:
        # work around scipy's SIGINT handling: https://stackoverflow.com/a/15472811
        import imp
        import ctypes
        import _thread
        import win32api

        if hook_sigint is None:
            hook_sigint = _thread.interrupt_main

        # Load the DLL manually to ensure its handler gets
        # set before our handler.
        #basepath = imp.find_module('numpy')[1]
        #ctypes.CDLL(os.path.join(basepath, 'core', 'libmmd.dll'))
        #ctypes.CDLL(os.path.join(basepath, 'core', 'libifcoremd.dll'))
        import numpy as np

        # Now set our handler for CTRL_C_EVENT. Other control event 
        # types will chain to the next handler.
        def handler(dwCtrlType, hook_sigint=hook_sigint):
            if dwCtrlType == 0: # CTRL_C_EVENT
                hook_sigint()
                return 1 # don't chain to the next handler
            return 0 # chain to the next handler

        win32api.SetConsoleCtrlHandler(handler, 1)
        return handler
Example #27
0
    def start(self, open_ui=False):
        async_fire_and_forget(self.async_start(open_ui=open_ui), self.loop)

        # Windows does not seem to handle Ctrl+C well so as a workaround
        # register a handler and manually stop the app
        if sys.platform == 'win32':
            import win32api

            def handle_win32_interrupt(sig, func=None):
                self.stop()
                return True

            win32api.SetConsoleCtrlHandler(handle_win32_interrupt, 1)

        try:
            self.loop.run_forever()
        except KeyboardInterrupt:
            self.loop.call_soon_threadsafe(self.loop.create_task,
                                           self.async_stop())
            self.loop.run_forever()
        except:
            # Catch all other exceptions and terminate the application. The loop
            # exeception handler will take care of logging the actual error and
            # LedFx will cleanly shutdown.
            self.loop.run_until_complete(self.async_stop(exit_code = -1))
            pass
        finally:
            self.loop.stop()
        return self.exit_code
Example #28
0
    def __register_signal_handlers(self):
        if operating_system.isWindows():
            import win32api  # pylint: disable=F0401

            def quit_adapter(*args):
                # The handler is called from something that is not the main thread, so we can't do
                # much wx-related
                event = threading.Event()

                def quit():
                    try:
                        self.quitApplication()
                    finally:
                        event.set()

                wx.CallAfter(quit)
                event.wait()
                return True

            win32api.SetConsoleCtrlHandler(quit_adapter, True)
        else:
            import signal

            def quit_adapter(*args):
                return self.quitApplication()

            signal.signal(signal.SIGTERM, quit_adapter)
            if hasattr(signal, 'SIGHUP'):
                forced_quit = lambda *args: self.quitApplication(force=True)
                signal.signal(signal.SIGHUP, forced_quit)  # pylint: disable=E1101
Example #29
0
File: run.py Project: yrnkrn/zapcc
    def execute_tests_in_pool(self, jobs, max_time):
        # We need to issue many wait calls, so compute the final deadline and
        # subtract time.time() from that as we go along.
        deadline = None
        if max_time:
            deadline = time.time() + max_time

        # Start a process pool. Copy over the data shared between all test runs.
        # FIXME: Find a way to capture the worker process stderr. If the user
        # interrupts the workers before we make it into our task callback, they
        # will each raise a KeyboardInterrupt exception and print to stderr at
        # the same time.
        pool = multiprocessing.Pool(
            jobs, worker_initializer,
            (self.lit_config, self.parallelism_semaphores))

        # Install a console-control signal handler on Windows.
        if win32api is not None:

            def console_ctrl_handler(type):
                print('\nCtrl-C detected, terminating.')
                pool.terminate()
                pool.join()
                abort_now()
                return True

            win32api.SetConsoleCtrlHandler(console_ctrl_handler, True)

        try:
            async_results = [
                pool.apply_async(worker_run_one_test,
                                 args=(test_index, test),
                                 callback=self.consume_test_result)
                for test_index, test in enumerate(self.tests)
            ]
            pool.close()

            # Wait for all results to come in. The callback that runs in the
            # parent process will update the display.
            for a in async_results:
                if deadline:
                    a.wait(deadline - time.time())
                else:
                    # Python condition variables cannot be interrupted unless
                    # they have a timeout. This can make lit unresponsive to
                    # KeyboardInterrupt, so do a busy wait with a timeout.
                    while not a.ready():
                        a.wait(1)
                if not a.successful():
                    a.get()  # Exceptions raised here come from the worker.
                if self.hit_max_failures:
                    break
        except:
            # Stop the workers and wait for any straggling results to come in
            # if we exited without waiting on every async result.
            pool.terminate()
            raise
        finally:
            pool.join()
Example #30
0
def registerSignals():
    signal.signal(signal.SIGINT, exitGracefully)
    signal.signal(signal.SIGTERM, exitGracefully)
    signal.signal(signal.SIGABRT, exitGracefully)
    signal.signal(signal.SIGSEGV, exitGracefully)
    signal.signal(signal.SIGILL, exitGracefully)
    signal.signal(signal.SIGFPE, exitGracefully)
    win32api.SetConsoleCtrlHandler(winExit, True)