changed to logging

This commit is contained in:
John Lancaster
2025-01-04 21:50:29 -06:00
parent 7ea362c5fd
commit f4aabf977e

View File

@@ -1,3 +1,4 @@
import logging
import threading import threading
from collections.abc import Iterable from collections.abc import Iterable
from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor
@@ -6,10 +7,12 @@ from queue import Queue
from time import perf_counter from time import perf_counter
from typing import Callable from typing import Callable
logger = logging.getLogger(__name__)
def worker(q: Queue): def worker(q: Queue):
thread = threading.current_thread() thread = threading.current_thread()
print(thread) logger.info(thread)
while True: while True:
try: try:
item = q.get() item = q.get()
@@ -17,25 +20,25 @@ def worker(q: Queue):
if item is None: if item is None:
break break
elif callable(item): elif callable(item):
print(f'Calling {item}') logger.info(f'Calling {item}')
item() item()
else:
print(f'{thread.name}: {item}')
except Exception as exc: except Exception as exc:
print(f'Error: {exc}') logger.info(f'Error: {exc}')
finally: finally:
proc_time = perf_counter() - get_time
q.task_done() q.task_done()
print(f'{proc_time*10**3:.3f}ms') proc_time = perf_counter() - get_time
logger.info(f'{thread.name}: {proc_time*10**3:.3f}ms {item}')
# print('Broke worker loop') # logger.info('Broke worker loop')
return f'Ended {thread.name}' return f'Ended {thread.name}'
def run_executor(queues: Iterable[Queue]): def run_executor(queues: Iterable[Queue]):
with ThreadPoolExecutor(thread_name_prefix='AD-App-Thread') as executor: with ThreadPoolExecutor(thread_name_prefix='AD-App-Thread') as executor:
for fut in executor.map(worker, queues): for q in queues:
print(fut) executor.submit(worker, q=q)
# for fut in executor.map(worker, queues):
# logger.info(fut)
def main(n: int, start: bool = True): def main(n: int, start: bool = True):
@@ -52,3 +55,24 @@ def main(n: int, start: bool = True):
def dispatch_worker(queue: Queue, func: Callable, *args, **kwargs): def dispatch_worker(queue: Queue, func: Callable, *args, **kwargs):
return queue.put_nowait(partial(func, *args, **kwargs)) return queue.put_nowait(partial(func, *args, **kwargs))
if __name__ == '__main__':
from time import sleep
logging.basicConfig(
level='INFO',
format='{asctime} [{levelname}] {message}',
style='{'
)
queues = main(5)
logger.info('Starting')
for i in range(5):
for q in queues:
q.put_nowait(i)
logger.info('Sleeping')
sleep(1.0)
logger.info('Done')