thread_stacksize1.py 980 B

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