Skip to content

Commit 5a65192

Browse files
committed
adding run example
1 parent ceecac6 commit 5a65192

File tree

2 files changed

+118
-1
lines changed

2 files changed

+118
-1
lines changed

log_example.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Author: Mike Ryan
4+
Date: 2024/05/16
5+
License: MIT
6+
"""
7+
import logging
8+
import multiprocessing
9+
import secrets
10+
import threading
11+
12+
from loguru import logger
13+
from tqdm import tqdm
14+
15+
from dsg_lib.common_functions import logging_config
16+
17+
# Configure logging as before
18+
logging_config.config_log(
19+
logging_directory='log',
20+
log_name='log',
21+
logging_level='DEBUG',
22+
log_rotation='100 MB',
23+
log_retention='10 days',
24+
log_backtrace=True,
25+
log_serializer=True,
26+
log_diagnose=True,
27+
# app_name='my_app',
28+
# append_app_name=True,
29+
intercept_standard_logging=True,
30+
enqueue=True,
31+
)
32+
33+
34+
# @logger.catch
35+
def div_zero(x, y):
36+
try:
37+
return x / y
38+
except ZeroDivisionError as e:
39+
logger.error(f'{e}')
40+
logging.error(f'{e}')
41+
42+
43+
# @logger.catch
44+
def div_zero_two(x, y):
45+
try:
46+
return x / y
47+
except ZeroDivisionError as e:
48+
logger.error(f'{e}')
49+
logging.error(f'{e}')
50+
51+
52+
def log_big_string(lqty=100, size=256):
53+
big_string = secrets.token_urlsafe(size)
54+
for _ in range(lqty):
55+
logging.debug(f'Lets make this a big message {big_string}')
56+
div_zero(x=1, y=0)
57+
div_zero_two(x=1, y=0)
58+
# after configuring logging
59+
# use loguru to log messages
60+
logger.debug('This is a loguru debug message')
61+
logger.info('This is an loguru info message')
62+
logger.error('This is an loguru error message')
63+
logger.warning('This is a loguru warning message')
64+
logger.critical('This is a loguru critical message')
65+
66+
# will intercept all standard logging messages also
67+
logging.debug('This is a standard logging debug message')
68+
logging.info('This is an standard logging info message')
69+
logging.error('This is an standard logging error message')
70+
logging.warning('This is a standard logging warning message')
71+
logging.critical('This is a standard logging critical message')
72+
73+
74+
def worker(wqty=1000, lqty=100, size=256):
75+
for _ in tqdm(range(wqty), ascii=True, leave=True): # Adjusted for demonstration
76+
log_big_string(lqty=lqty, size=size)
77+
78+
79+
def main(wqty: int = 100, lqty: int = 10, size: int = 256, workers: int = 16, thread_test: bool = False, process_test: bool = False):
80+
if process_test:
81+
processes = []
82+
# Create worker processes
83+
for _ in tqdm(range(workers), desc="Multi-Processing Start", leave=True):
84+
p = multiprocessing.Process(
85+
target=worker, args=(wqty, lqty, size,))
86+
processes.append(p)
87+
p.start()
88+
89+
for p in tqdm((processes), desc="Multi-Processing Start", leave=False):
90+
p.join(timeout=60) # Timeout after 60 seconds
91+
if p.is_alive():
92+
logger.error(f"Process {p.name} is hanging. Terminating.")
93+
p.terminate()
94+
p.join()
95+
96+
if thread_test:
97+
threads = []
98+
for _ in tqdm(range(workers), desc="Threading Start", leave=True): # Create worker threads
99+
t = threading.Thread(target=worker, args=(wqty, lqty, size,))
100+
threads.append(t)
101+
t.start()
102+
103+
for t in tqdm(threads, desc="Threading Gather", leave=False):
104+
t.join()
105+
106+
107+
if __name__ == "__main__":
108+
from time import time
109+
start = time()
110+
main(wqty=20, lqty=100, size=64, workers=8, thread_test=False, process_test=True)
111+
print(f"Execution time: {time()-start:.2f} seconds")

makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Variables
2-
REPONAME = PyJSONSchemaForm
2+
REPONAME = devsetgo_lib
33

44
PYTHON = python3
55
PIP = $(PYTHON) -m pip
@@ -96,3 +96,9 @@ ruff: ## Format Python code with Ruff
9696
ruff check --fix --exit-non-zero-on-fix --show-fixes $(SERVICE_PATH)
9797
ruff check --fix --exit-non-zero-on-fix --show-fixes $(TESTS_PATH)
9898
ruff check --fix --exit-non-zero-on-fix --show-fixes $(EXAMPLE_PATH)
99+
100+
101+
example-log: ## Run the example logging script
102+
cp /workspaces/devsetgo_lib/examples/log_example.py /workspaces/devsetgo_lib/
103+
python3 log_example.py
104+
cp /workspaces/devsetgo_lib/log_example.py /workspaces/devsetgo_lib/examples

0 commit comments

Comments
 (0)