thread_qstr1.py 895 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. # test concurrent interning of strings
  2. #
  3. # MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
  4. try:
  5. import utime as time
  6. except ImportError:
  7. import time
  8. import _thread
  9. # function to check the interned string
  10. def check(s, val):
  11. assert type(s) == str
  12. assert int(s) == val
  13. # main thread function
  14. def th(base, n):
  15. for i in range(n):
  16. # this will intern the string and check it
  17. exec("check('%u', %u)" % (base + i, base + i))
  18. with lock:
  19. global n_finished
  20. n_finished += 1
  21. lock = _thread.allocate_lock()
  22. n_thread = 4
  23. n_finished = 0
  24. n_qstr_per_thread = 100 # make 1000 for a more stressful test (uses more heap)
  25. # spawn threads
  26. for i in range(n_thread):
  27. _thread.start_new_thread(th, (i * n_qstr_per_thread, n_qstr_per_thread))
  28. # wait for threads to finish
  29. while n_finished < n_thread:
  30. time.sleep(1)
  31. print('pass')