|
2 | 2 | import threading |
3 | 3 | import gc |
4 | 4 |
|
5 | | -# Set limit to avoid crash my computer in worst case if my code is incorrect some how |
6 | | -resource.setrlimit(resource.RLIMIT_AS, (100_000_000, 150_000_000)) |
7 | | - |
8 | 5 | def handler(): |
9 | 6 | pass |
10 | 7 |
|
11 | 8 | def serving(): |
12 | | - hard_limit = 25_000_000 # This can be change depending of the Python version. |
13 | | - step_limit_reduction = 1000 |
14 | | - for _ in range(100_000): |
15 | | - gc.collect(2) # Force to have a more determist issue by getting back heap memory |
| 9 | + # These should be tweak (depending of Python version + system) |
| 10 | + HARD_LIMIT_START = 30_000_000 |
| 11 | + LIMIT_REDUCTION = 5_000 |
| 12 | + |
| 13 | + for _ in range(500_000): |
| 14 | + gc.collect(2) # Force getting back memory: seems to increase the determinism of the script |
16 | 15 |
|
17 | 16 | # Limit the heap size available for this process |
18 | | - resource.setrlimit(resource.RLIMIT_DATA, (hard_limit, hard_limit)) |
| 17 | + resource.setrlimit(resource.RLIMIT_DATA, (HARD_LIMIT_START, HARD_LIMIT_START * 2)) |
19 | 18 | try: |
20 | | - t = threading.Thread(target=handler) |
21 | | - print(f'Start Thread: {t} - Heap size limit : {hard_limit}') |
22 | | - t.start() |
23 | | - t.join() |
24 | | - hard_limit -= step_limit_reduction |
25 | | - except RuntimeError as r: # If we fail completly to start the new thread |
26 | | - print(f'RuntimeError {t} : {r}',) |
| 19 | + handler_thread = threading.Thread(target=handler) |
| 20 | + print(f'Start Thread: {handler_thread} - Heap size limit : {HARD_LIMIT_START}') |
| 21 | + handler_thread.start() |
| 22 | + handler_thread.join() |
| 23 | + HARD_LIMIT_START -= LIMIT_REDUCTION |
| 24 | + except RuntimeError as r: # If Python refused to launch a new Thread |
| 25 | + print(f'RuntimeError: {r} - Cannot start the thread at all => error not detected.') |
27 | 26 | return |
28 | 27 |
|
29 | | -print('Start Serving') |
30 | | -serving_thread = threading.Thread(target=serving, daemon=True) |
| 28 | +serving_thread = threading.Thread(target=serving) |
31 | 29 | serving_thread.start() |
32 | 30 | serving_thread.join() |
33 | | - |
0 commit comments