1# test setting the thread stack size 2# 3# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd 4try: 5 import usys as sys 6except ImportError: 7 import sys 8import _thread 9 10# different implementations have different minimum sizes 11if sys.implementation.name == "micropython": 12 sz = 2 * 1024 13else: 14 sz = 512 * 1024 15 16 17def foo(): 18 pass 19 20 21def thread_entry(): 22 foo() 23 with lock: 24 global n_finished 25 n_finished += 1 26 27 28# reset stack size to default 29_thread.stack_size() 30 31# test set/get of stack size 32print(_thread.stack_size()) 33print(_thread.stack_size(sz)) 34print(_thread.stack_size() == sz) 35print(_thread.stack_size()) 36 37lock = _thread.allocate_lock() 38n_thread = 2 39n_finished = 0 40 41# set stack size and spawn a few threads 42_thread.stack_size(sz) 43for i in range(n_thread): 44 _thread.start_new_thread(thread_entry, ()) 45 46# reset stack size to default (for subsequent scripts on baremetal) 47_thread.stack_size() 48 49# busy wait for threads to finish 50while n_finished < n_thread: 51 pass 52print("done") 53