2017-02-04 12:35:08 +00:00
|
|
|
# stress test for creating many threads
|
|
|
|
|
|
|
|
try:
|
2021-05-08 09:20:05 +01:00
|
|
|
import utime
|
|
|
|
|
|
|
|
sleep_ms = utime.sleep_ms
|
2017-02-04 12:35:08 +00:00
|
|
|
except ImportError:
|
|
|
|
import time
|
2021-05-08 09:20:05 +01:00
|
|
|
|
|
|
|
sleep_ms = lambda t: time.sleep(t / 1000)
|
2017-02-04 12:35:08 +00:00
|
|
|
import _thread
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2017-02-04 12:35:08 +00:00
|
|
|
def thread_entry(n):
|
|
|
|
pass
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2017-02-04 12:35:08 +00:00
|
|
|
thread_num = 0
|
|
|
|
while thread_num < 500:
|
|
|
|
try:
|
|
|
|
_thread.start_new_thread(thread_entry, (thread_num,))
|
|
|
|
thread_num += 1
|
2021-05-08 09:20:05 +01: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 12:35:08 +00:00
|
|
|
|
|
|
|
# wait for the last threads to terminate
|
2021-05-08 09:20:05 +01:00
|
|
|
sleep_ms(500)
|
2017-02-04 12:35:08 +00:00
|
|
|
print("done")
|