Skip to content

Commit 6ab8f76

Browse files
committed
python issue
1 parent c516f67 commit 6ab8f76

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

python_issue_mini.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,29 @@
22
import threading
33
import gc
44

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-
85
def handler():
96
pass
107

118
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
1615

1716
# 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))
1918
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.')
2726
return
2827

29-
print('Start Serving')
30-
serving_thread = threading.Thread(target=serving, daemon=True)
28+
serving_thread = threading.Thread(target=serving)
3129
serving_thread.start()
3230
serving_thread.join()
33-

python_issue_mini_2.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@
88
def memory_error():
99
memory_issue_list = []
1010
while True:
11-
memory_issue_list.append("a" * 45000)
11+
memory_issue_list.append("a" * 50_000)
1212

1313
def handler():
1414
time.sleep(0.001)
1515
try:
1616
memory_error()
1717
except MemoryError:
18-
print(f"MemoryError in {threading.current_thread()}")
18+
print(f"MemoryError in {threading.current_thread()} - Stop")
1919

2020
def serving():
2121
for _ in range(100):
2222
try:
2323
t = threading.Thread(target=handler)
2424
print(f'Start Thread: {t}')
2525
t.start()
26-
except RuntimeError:
27-
print('RuntimeError', {t})
26+
except RuntimeError as r:
27+
print(f'RuntimeError: {r} - Cannot start the thread at all => error not detected.')
2828

2929
print('Start Serving')
3030
serving_thread = threading.Thread(target=serving, daemon=True)

0 commit comments

Comments
 (0)