Best Python code snippet using molotov_python
runner.py
Source:runner.py  
...104            self._process()105        return self._results106    def _shutdown(self, signal, frame):107        stop()108        self._kill_tasks()109        # send sigterms110        for proc in self._procs:111            proc.terminate()112    def _runner(self):113        args = self.args114        def _prepare():115            tasks = []116            delay = 0117            if args.ramp_up > 0.0:118                step = args.ramp_up / args.workers119            else:120                step = 0.0121            for i in range(self.args.workers):122                worker = Worker(123                    i,124                    self._results,125                    self.console,126                    self.args,127                    self.statsd,128                    delay,129                    self.loop,130                )131                f = self.ensure_future(worker.run())132                tasks.append(f)133                delay += step134            return tasks135        if self.args.quiet:136            return _prepare()137        else:138            msg = "Preparing {} worker{}"139            msg = msg.format(args.workers, "s" if args.workers > 1 else "")140            return self.console.print_block(msg, _prepare)141    def _process(self):142        set_timer()143        # coroutine that will kill everything when duration is up144        if self.args.duration and self.args.force_shutdown:145            async def _duration_killer():146                cancelled = object()147                res = await cancellable_sleep(self.args.duration, result=cancelled)148                if res is cancelled or (res and not res.canceled()):149                    self._shutdown(None, None)150                    await asyncio.sleep(0)151            _duration_killer = self.ensure_future(_duration_killer())152        else:153            _duration_killer = None154        if self.args.processes > 1:155            signal.signal(signal.SIGINT, self._shutdown)156            signal.signal(signal.SIGTERM, self._shutdown)157            self.loop = asyncio.new_event_loop()158            asyncio.set_event_loop(self.loop)159        if self.args.debug:160            self.console.print("**** RUNNING IN DEBUG MODE == SLOW ****")161            self.loop.set_debug(True)162        self._set_statsd()163        if self.statsd is not None:164            self._tasks.append(self.ensure_future(self.statsd.connect()))165        if self.args.original_pid == os.getpid():166            self._tasks.append(self.ensure_future(self._send_workers_event(1)))167            if not self.args.quiet:168                fut = self._display_results(self.args.console_update)169                update = self.ensure_future(fut)170                display = self.ensure_future(self.console.display())171                display = self.gather(update, display)172                self._tasks.append(display)173        workers = self.gather(*self._runner())174        def _stop(cb):175            if _duration_killer is not None:176                if not _duration_killer.done():177                    _duration_killer.cancel()178            stop()179        workers.add_done_callback(_stop)180        self._tasks.append(workers)181        try:182            self.loop.run_until_complete(self.gather(*self._tasks))183        finally:184            if self.statsd is not None:185                self.loop.run_until_complete(self.ensure_future(self.statsd.close()))186            self._kill_tasks()187            self.loop.close()188    def _kill_tasks(self):189        cancellable_sleep.cancel_all()190        for task in reversed(self._tasks):191            with suppress(asyncio.CancelledError):192                task.cancel()193        for task in self._tasks:194            del task195        self._tasks[:] = []196    def display_results(self):197        ok, fail = self._results["OK"].value, self._results["FAILED"].value198        workers = self._results["WORKER"].value199        pat = "SUCCESSES: %s | FAILURES: %s | WORKERS: %s"200        return pat % (ok, fail, workers)201    async def _display_results(self, update_interval):202        while not is_stopped():...RawServer.py
Source:RawServer.py  
...112        try:113            while not self.doneflag.isSet():114                try:115                    self.pop_external()116                    self._kill_tasks()117                    if self.funcs:118                        period = self.funcs[0][0] + 0.001 - clock()119                    else:120                        period = 2 ** 30121                    if period < 0:122                        period = 0123                    events = self.sockethandler.do_poll(period)124                    if self.doneflag.isSet():125                        return126                    while self.funcs and self.funcs[0][0] <= clock():127                        garbage1, func, id = self.funcs.pop(0)128                        if id in self.tasks_to_kill:129                            pass130                        try:131#                            print func.func_name132                            func()133                        except (SystemError, MemoryError), e:134                            self.failfunc(str(e))135                            return136                        except KeyboardInterrupt:137#                            self.exception(True)138                            return139                        except:140                            if self.noisy:141                                self.exception()142                    self.sockethandler.close_dead()143                    self.sockethandler.handle_events(events)144                    if self.doneflag.isSet():145                        return146                    self.sockethandler.close_dead()147                except (SystemError, MemoryError), e:148                    self.failfunc(str(e))149                    return150                except error:151                    if self.doneflag.isSet():152                        return153                except KeyboardInterrupt:154#                    self.exception(True)155                    return156                except:157                    self.exception()158                if self.exccount > 10:159                    return160        finally:161#            self.sockethandler.shutdown()162            self.finished.set()163164    def is_finished(self):165        return self.finished.isSet()166167    def wait_until_finished(self):168        self.finished.wait()169170    def _kill_tasks(self):171        if self.tasks_to_kill:172            new_funcs = []173            for (t, func, id) in self.funcs:174                if id not in self.tasks_to_kill:175                    new_funcs.append((t, func, id))176            self.funcs = new_funcs177            self.tasks_to_kill = []178179    def kill_tasks(self, id):180        self.tasks_to_kill.append(id)181182    def exception(self, kbint = False):183        if not kbint:184            self.excflag.set()
...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
