Best Python code snippet using molotov_python
runner.py
Source:runner.py  
...35            "SETUP_FAILED",36            "SESSION_SETUP_FAILED",37        )38        self.eventer = EventSender(self.console)39    def _set_statsd(self):40        if self.args.statsd:41            self.statsd = get_statsd_client(self.args.statsd_address)42        else:43            self.statsd = None44    def run_coro(self, coro):45        if not self.loop.is_running():46            raise Exception("Loop is not running")47        future = asyncio.run_coroutine_threadsafe(coro, self.loop)48        return future.result()49    def gather(self, *futures):50        return asyncio.gather(*futures, loop=self.loop, return_exceptions=True)51    def ensure_future(self, coro):52        return asyncio.ensure_future(coro, loop=self.loop)53    def __call__(self):54        global_setup = get_fixture("global_setup")55        if global_setup is not None:56            try:57                global_setup(self.args)58            except Exception as e:59                self.console.print("The global_setup() fixture failed")60                self.console.print_error(e)61                raise62        try:63            return self._launch_processes()64        finally:65            global_teardown = get_fixture("global_teardown")66            if global_teardown is not None:67                try:68                    global_teardown()69                except Exception as e:70                    # we can't stop the teardown process71                    self.console.print_error(e)72    def _launch_processes(self):73        args = self.args74        signal.signal(signal.SIGINT, self._shutdown)75        signal.signal(signal.SIGTERM, self._shutdown)76        args.original_pid = os.getpid()77        if args.processes > 1:78            if not args.quiet:79                self.console.print("Forking %d processes" % args.processes)80            jobs = []81            for i in range(args.processes):82                p = multiprocessing.Process(target=self._process)83                jobs.append(p)84                p.start()85            for job in jobs:86                self._procs.append(job)87            async def run(quiet, console):88                while len(self._procs) > 0:89                    if not quiet:90                        console.print(self.display_results(), end="\r")91                    for job in jobs:92                        if job.exitcode is not None and job in self._procs:93                            self._procs.remove(job)94                    await cancellable_sleep(args.console_update)95                await self.console.stop()96                await self.eventer.stop()97            tasks = [98                self.ensure_future(self.console.display()),99                self.ensure_future(self._send_workers_event(1)),100                self.ensure_future(run(args.quiet, self.console)),101            ]102            self.loop.run_until_complete(self.gather(*tasks))103        else: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():...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!!
