Best Python code snippet using localstack_python
process_manager.py
Source:process_manager.py
1import logging2import os3from multiprocessing import Queue, active_children, get_start_method4from threading import Condition, Lock5from PyQt5.QtCore import QObject, pyqtSignal6from gridplayer.multiprocess.command_loop import CommandLoopThreaded7from gridplayer.settings import Settings8from gridplayer.utils.log_config import QueueListenerRoot9from gridplayer.utils.misc import force_terminate_children, force_terminate_children_all10class PlayerInstance(object):11 def __init__(self, instance, player_id):12 self._instance = instance13 self._player_id = player_id14 def get_player_shared_data(self):15 return self._instance.get_player_shared_data(self._player_id)16 def cleanup(self):17 self._instance.cleanup_player_shared_data(self._player_id)18class ProcessManager(CommandLoopThreaded, QObject):19 crash = pyqtSignal(str)20 def __init__(self, instance_class, **kwargs):21 super().__init__(**kwargs)22 self.crash_func = self.crash_all23 self.instances_lock = Lock()24 self.instances = {}25 self.dying_instances = set()26 self._instances_killed = Condition()27 self._limit = Settings().get("player/video_driver_players")28 self._instance_class = instance_class29 # Prevent opening multiple resource trackers30 if os.name != "nt":31 from multiprocessing.resource_tracker import ensure_running # noqa: WPS43332 ensure_running()33 self.cmd_loop_start_thread()34 # When forking log config is inherited by children35 # When spawning log must be handled manually36 if get_start_method() == "fork":37 self._log_queue = None38 self._log_listener = None39 else:40 self._log_queue = Queue(-1)41 self._log_listener = QueueListenerRoot(self._log_queue)42 self._log_listener.start()43 self._log = logging.getLogger(self.__class__.__name__)44 @property45 def active_instances(self):46 active_instances = []47 with self.instances_lock:48 for instance in self.instances.values():49 with instance.is_dead.get_lock():50 if not instance.is_dead.value:51 active_instances.append(instance) # noqa: WPS22052 return active_instances53 def get_instance(self):54 instance = self._get_available_instance()55 if instance is None:56 instance = self.create_instance()57 with self.instances_lock:58 self.instances[instance.id] = instance59 self._log.debug(f"Launching process {instance.id}")60 instance.process.start()61 return instance62 def create_instance(self):63 instance = self._instance_class(64 players_per_instance=self._limit, pm_callback_pipe=self._self_pipe65 )66 if self._log_queue:67 log_level = Settings().get("logging/log_level")68 instance.setup_log_queue(self._log_queue, log_level)69 return instance70 def init_player(self, init_data, pipe):71 instance = self.get_instance()72 player_id = instance.request_new_player(init_data, pipe)73 return PlayerInstance(instance, player_id)74 def cleanup_instance(self, inst_id):75 with self.instances_lock:76 instance = self.instances.pop(inst_id)77 self.dying_instances.add(inst_id)78 instance.process.join()79 instance.process.close()80 self.dying_instances.remove(inst_id)81 with self._instances_killed:82 if not self.instances and not self.dying_instances:83 self._instances_killed.notify()84 def crash_all(self, traceback_txt):85 force_terminate_children_all()86 self.crash.emit(traceback_txt)87 def cleanup(self):88 self._log.debug("Waiting for processes to shut down...")89 with self._instances_killed:90 self._instances_killed.wait_for(lambda: not self.instances, timeout=5)91 if active_children():92 self._log.warning("Force terminating child processes...")93 force_terminate_children()94 self._log.debug("Terminating command loop...")95 self.cmd_loop_terminate()96 if self._log_listener is not None:97 self._log.debug("Terminating log listener...")98 self._log_listener.stop()99 def set_log_level(self, log_level):100 for a in self.active_instances:101 a.request_set_log_level(log_level)102 def _get_available_instance(self):103 for instance in self.active_instances:104 with instance.player_count.get_lock():105 if instance.player_count.value < self._limit:106 return instance...
log.py
Source:log.py
1"""2Logging utilities.3"""4import sys5import logging6from logging.handlers import QueueListener7import multiprocessing as mp8_log = logging.getLogger(__name__)9_lts_initialized = False10_ltn_initialized = False11_log_queue = None12_log_listener = None13class InjectHandler:14 "Handler that re-injects a message into parent process logging"15 level = logging.DEBUG16 def handle(self, record):17 logger = logging.getLogger(record.name)18 if logger.isEnabledFor(record.levelno):19 logger.handle(record)20class LowPassFilter:21 def filter(record):22 return record.levelno < logging.WARNING23def log_to_stderr(level=logging.INFO):24 """25 Set up the logging infrastructure to show log output on ``sys.stderr``, where it will26 appear in the IPython message log.27 """28 global _lts_initialized29 if _lts_initialized:30 _log.info('log already initialized')31 h = logging.StreamHandler(sys.stderr)32 f = logging.Formatter('[%(levelname)7s] %(name)s %(message)s')33 h.setFormatter(f)34 root = logging.getLogger()35 root.addHandler(h)36 root.setLevel(level)37 _log.info('stderr logging configured')38 _lts_initialized = True39def log_to_notebook(level=logging.INFO):40 """41 Set up the logging infrastructure to show log output in the Jupyter notebook.42 """43 global _ltn_initialized44 if _ltn_initialized:45 _log.info('log already initialized')46 h = logging.StreamHandler(sys.stderr)47 f = logging.Formatter('[%(levelname)7s] %(name)s %(message)s')48 h.setFormatter(f)49 h.setLevel(logging.WARNING)50 oh = logging.StreamHandler(sys.stdout)51 oh.setFormatter(f)52 oh.addFilter(LowPassFilter)53 root = logging.getLogger()54 root.addHandler(h)55 root.addHandler(oh)56 root.setLevel(level)57 _log.info('notebook logging configured')58 _ltn_initialized = True59def log_queue():60 """61 Get the log queue for child process logging.62 """63 global _log_queue, _log_listener64 from lenskit.util.parallel import LKContext65 ctx = LKContext.INSTANCE66 if _log_queue is None:67 _log_queue = ctx.Queue()68 _log_listener = QueueListener(_log_queue, InjectHandler())69 _log_listener.start()...
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!!