Last active
February 25, 2025 14:53
-
-
Save matthewpoer/6c5f37f433b7b26053dcb025d07cdaf9 to your computer and use it in GitHub Desktop.
Python Threading example code
This file contains hidden or 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
import concurrent.futures | |
from http.client import HTTPException | |
import threading | |
import time | |
def output(message): | |
print(f"{time.time_ns()}: {message}") | |
invalid_item_names = ["item4", "item6"] | |
fatal_item_names = ["item2"] | |
def validate_item(item: dict) -> list: | |
output(f"validate_item(): {item["name"]}") | |
time.sleep(2) | |
validation_err = [] | |
if(item["name"] in invalid_item_names): | |
message = f"{item["name"]} is invalid" | |
output(message) | |
validation_err.append(message) | |
return validation_err | |
if(item["name"] in fatal_item_names): | |
message = f"{item["name"]} is fatal" | |
output(message) | |
raise HTTPException(message) | |
output(f"{item["name"]} sleeping for {item["sleep-for"]} seconds") | |
time.sleep(item["sleep-for"]) | |
output(f"{item["name"]} sleep complete") | |
return validation_err | |
def validate_items(items: dict) -> list: | |
error_list = [] | |
error_list_lock = threading.Lock() | |
def validate_item_wrapper(item: str): | |
validation_err = None | |
try: | |
validation_err = validate_item(item) | |
except Exception as err: | |
output(f"validate_item_wrapper exception for item: {item["name"]}") | |
validation_err = ["There was an internal error, please see logs for details"] | |
with error_list_lock: | |
error_list.extend(validation_err) | |
# error_list.extend(validation_err) | |
num_workers = 3 | |
with concurrent.futures.ThreadPoolExecutor(max_workers=num_workers) as executor: | |
for item in items: | |
executor.submit(validate_item_wrapper, item) | |
executor.shutdown() | |
output(f"error_list: {error_list}") | |
items = [ | |
{"name": "item1", "sleep-for": 10}, | |
{"name": "item2", "sleep-for": 10}, | |
{"name": "item3", "sleep-for": 10}, | |
{"name": "item4", "sleep-for": 10}, | |
{"name": "item5", "sleep-for": 10}, | |
{"name": "item6", "sleep-for": 10}, | |
{"name": "item7", "sleep-for": 10}, | |
] | |
validate_items(items) |
This file contains hidden or 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
% python3 example-python-threads.py | |
1740495021346587000: validate_item(): item1 | |
1740495021346692000: validate_item(): item2 | |
1740495021346771000: validate_item(): item3 | |
1740495023350515000: item3 sleeping for 10 seconds | |
1740495023350597000: item1 sleeping for 10 seconds | |
1740495023350618000: item2 is fatal | |
1740495023350872000: validate_item_wrapper exception for item: item2 | |
1740495023351049000: validate_item(): item4 | |
1740495025358058000: item4 is invalid | |
1740495025358373000: validate_item(): item5 | |
1740495027359090000: item5 sleeping for 10 seconds | |
1740495033355978000: item3 sleep complete | |
1740495033356063000: item1 sleep complete | |
1740495033356317000: validate_item(): item6 | |
1740495033356442000: validate_item(): item7 | |
1740495035361599000: item7 sleeping for 10 seconds | |
1740495035361682000: item6 is invalid | |
1740495037364423000: item5 sleep complete | |
1740495045366882000: item7 sleep complete | |
1740495045367448000: error_list: ['There was an internal error, please see logs for details', 'item4 is invalid', 'item6 is invalid'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment