Last active
February 16, 2024 16:31
-
-
Save kienanstewart/879bd3bf19d852653b70a3c42caef361 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Copyright (C) 2024 Kienan Stewart <kstewart@efficios.com> | |
# SPDX-License-Identifer: LGPL-2.1-only | |
import lttngust | |
import logging | |
import queue | |
import signal | |
import sys | |
import threading | |
import time | |
threads = {} | |
thread_max = 24 | |
main_logger = None | |
should_quit = False | |
def run(logger, signal_queue): | |
tid = threading.get_ident() | |
while signal_queue.empty(): | |
logger.debug('[{}] debug'.format(tid)) | |
logger.info('[{}] info'.format(tid)) | |
logger.warning('[{}] warn'.format(tid)) | |
logger.error('[{}] warn'.format(tid)) | |
logger.critical('[{}] critical'.format(tid)) | |
logger.info('[{}] consuming signal'.format(tid)) | |
signal_queue.get() | |
logger.info('[{}] ending'.format(tid)) | |
def quit_handler(signum, frame): | |
global should_quit | |
start = time.time() | |
for i, obj in threads.items(): | |
main_logger.info('Putting into queue for thread {} [{}]'.format(i, obj['thread'].ident)) | |
obj['queue'].put(True) | |
main_logger.info('Put done {} [{}]'.format(i, obj['thread'].ident)) | |
all_joined = False | |
while not all_joined: | |
any_alive = False | |
for i, obj in threads.items(): | |
obj['thread'].join() | |
if obj['thread'].is_alive(): | |
any_alive = True | |
break | |
if not any_alive: | |
all_joined = True | |
break | |
time.sleep(1) | |
main_logger.info('Done') | |
should_quit = True | |
print('Teardown took {}s'.format(time.time() - start)) | |
if __name__ == '__main__': | |
logging.basicConfig(level=logging.INFO) | |
main_logger = logging.getLogger('main') | |
logger = logging.getLogger('tp') | |
signal.signal(signal.SIGTERM, quit_handler) | |
for i in range(thread_max): | |
q = queue.Queue() | |
t = threading.Thread(target=run, args=(logger, q)) | |
threads[i] = { | |
'queue': q, | |
'thread': t, | |
} | |
main_logger.info('About to start thread #{}'.format(i)) | |
t.start() | |
main_logger.critical('Started thread {}'.format(t.ident)) | |
while not should_quit: | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment