Best Python code snippet using slash
command.py
Source:command.py
...28class AbortException(Exception):29 """Indicates early abort on SIGINT, SIGTERM or internal hard timeout."""30 pass31@contextmanager32def handle_sigterm(process, abort_fun, enabled):33 """Call`abort_fun` on sigterm and restore previous handler to prevent34 erroneous termination of an already terminated process.35 Args:36 process: The process to terminate.37 abort_fun: Function taking two parameters: the process to terminate and38 an array with a boolean for storing if an abort occured.39 enabled: If False, this wrapper will be a no-op.40 """41 # Variable to communicate with the signal handler.42 abort_occured = [False]43 def handler(signum, frame):44 abort_fun(process, abort_occured)45 if enabled:46 previous = signal.signal(signal.SIGTERM, handler)47 try:48 yield49 finally:50 if enabled:51 signal.signal(signal.SIGTERM, previous)52 if abort_occured[0]:53 raise AbortException()54class BaseCommand(object):55 def __init__(self, shell, args=None, cmd_prefix=None, timeout=60, env=None,56 verbose=False, resources_func=None, handle_sigterm=False):57 """Initialize the command.58 Args:59 shell: The name of the executable (e.g. d8).60 args: List of args to pass to the executable.61 cmd_prefix: Prefix of command (e.g. a wrapper script).62 timeout: Timeout in seconds.63 env: Environment dict for execution.64 verbose: Print additional output.65 resources_func: Callable, returning all test files needed by this command.66 handle_sigterm: Flag indicating if SIGTERM will be used to terminate the67 underlying process. Should not be used from the main thread, e.g. when68 using a command to list tests.69 """70 assert(timeout > 0)71 self.shell = shell72 self.args = args or []73 self.cmd_prefix = cmd_prefix or []74 self.timeout = timeout75 self.env = env or {}76 self.verbose = verbose77 self.handle_sigterm = handle_sigterm78 def execute(self):79 if self.verbose:80 print('# %s' % self)81 process = self._start_process()82 with handle_sigterm(process, self._abort, self.handle_sigterm):83 # Variable to communicate with the timer.84 timeout_occured = [False]85 timer = threading.Timer(86 self.timeout, self._abort, [process, timeout_occured])87 timer.start()88 start_time = time.time()89 stdout, stderr = process.communicate()90 duration = time.time() - start_time91 timer.cancel()92 return output.Output(93 process.returncode,94 timeout_occured[0],95 stdout.decode('utf-8', 'replace').encode('utf-8'),96 stderr.decode('utf-8', 'replace').encode('utf-8'),...
main.py
Source:main.py
...4from signal import signal, SIGTERM, SIGINT5def main():6 listener = RabbitListener(SkService())7 listener.run()8 def handle_sigterm(*_):9 print("Shutdown signal received")10 listener.close()11 print("Shutdown gracefully")12 signal(SIGTERM, handle_sigterm)13 signal(SIGINT, handle_sigterm)14 try:15 listener.close()16 except KeyboardInterrupt:17 handle_sigterm()18if __name__ == '__main__':19 DI()...
sigterm.py
Source:sigterm.py
...5 Used to exit quickly from docker containers6 https://lemanchet.fr/articles/gracefully-stop-python-docker-container.html7 """8 import signal9 def handle_sigterm(*args):10 raise KeyboardInterrupt()
...
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!!