File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- from home_guardian .configuration .loguru import configure
1+ from home_guardian .configuration .loguru_configuration import (
2+ configure as loguru_configure ,
3+ )
4+ from home_guardian .configuration .thread_pool_configuration import (
5+ configure as thread_pool_configure ,
6+ )
27
3- configure ()
8+ loguru_configure ()
9+ thread_pool_configure ()
Original file line number Diff line number Diff line change 11import random
22
3+ from home_guardian .configuration .thread_pool_configuration import simulate_multi_thread
34from home_guardian .repository .user_repository import create_user
45
56if __name__ == "__main__" :
67 create_user (f"user{ random .random ()} " )
8+ simulate_multi_thread ()
File renamed without changes.
Original file line number Diff line number Diff line change 1+ import time
2+ from concurrent .futures import ThreadPoolExecutor
3+
4+ from loguru import logger
5+
6+ from home_guardian .function_collection import get_cpu_count
7+
8+ max_workers = 2 * get_cpu_count ()
9+ executor = ThreadPoolExecutor (
10+ max_workers = max_workers , thread_name_prefix = "my_thread_pool"
11+ )
12+
13+
14+ def configure () -> None :
15+ """
16+ Configure thread pool.
17+ """
18+ logger .warning (
19+ f"Thread pool executor with { max_workers } workers, executor: { executor } "
20+ )
21+
22+
23+ def simulate_get_html (times ):
24+ time .sleep (times )
25+ logger .info (f"get page { times } finished" )
26+ return times
27+
28+
29+ def simulate_multi_thread () -> None :
30+ task1 = executor .submit (simulate_get_html , 3 )
31+ task2 = executor .submit (simulate_get_html , 2 )
32+ logger .info (task1 .done ())
33+ logger .info (task2 .cancel ())
34+ time .sleep (4 )
35+ logger .info (task1 .done ())
36+ logger .info (task1 .result ())
Original file line number Diff line number Diff line change @@ -17,3 +17,14 @@ def get_data_dir() -> str:
1717 data_dir = f"{ get_root_path ()} /_data"
1818 os .makedirs (data_dir , exist_ok = True )
1919 return data_dir
20+
21+
22+ def get_cpu_count () -> int :
23+ """
24+ Get CPU count, default is 4
25+ """
26+ cpu_count = os .cpu_count ()
27+ if cpu_count is not None :
28+ return cpu_count
29+ else :
30+ return 4
You can’t perform that action at this time.
0 commit comments