2017-02-04 07:35:08 -05:00
|
|
|
# stress test for creating many threads
|
|
|
|
|
2022-08-18 02:57:45 -04:00
|
|
|
import time
|
2021-05-08 04:20:05 -04:00
|
|
|
|
2022-08-18 02:57:45 -04:00
|
|
|
if hasattr(time, "sleep_ms"):
|
|
|
|
sleep_ms = time.sleep_ms
|
|
|
|
else:
|
2021-05-08 04:20:05 -04:00
|
|
|
sleep_ms = lambda t: time.sleep(t / 1000)
|
2022-08-18 02:57:45 -04:00
|
|
|
|
2017-02-04 07:35:08 -05:00
|
|
|
import _thread
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2017-02-04 07:35:08 -05:00
|
|
|
def thread_entry(n):
|
|
|
|
pass
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2017-02-04 07:35:08 -05:00
|
|
|
thread_num = 0
|
|
|
|
while thread_num < 500:
|
|
|
|
try:
|
|
|
|
_thread.start_new_thread(thread_entry, (thread_num,))
|
|
|
|
thread_num += 1
|
2021-05-08 04:20:05 -04:00
|
|
|
except (MemoryError, OSError) as er:
|
|
|
|
# Cannot create a new thead at this stage, yield for a bit to
|
|
|
|
# let existing threads run to completion and free up resources.
|
|
|
|
sleep_ms(50)
|
2017-02-04 07:35:08 -05:00
|
|
|
|
|
|
|
# wait for the last threads to terminate
|
2021-05-08 04:20:05 -04:00
|
|
|
sleep_ms(500)
|
2017-02-04 07:35:08 -05:00
|
|
|
print("done")
|