1
0

98_thread.py 956 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from pkpy import configmacros
  2. if configmacros['PK_ENABLE_THREADS'] == 1:
  3. from pkpy import ComputeThread
  4. else:
  5. print('threads is not enabled, skipping test...')
  6. exit()
  7. import time
  8. thread_1 = ComputeThread(1)
  9. thread_2 = ComputeThread(2)
  10. for t in [thread_1, thread_2]:
  11. t.exec('''
  12. def func(a):
  13. from pkpy import currentvm
  14. print("Hello from thread", currentvm(), "a =", a)
  15. for i in range(500000):
  16. if i % 100000 == 0:
  17. print(i, "from thread", currentvm())
  18. return a
  19. x = 123
  20. ''')
  21. assert thread_1.eval('x') == 123
  22. # thread_1.wait_for_done()
  23. # thread_2.wait_for_done()
  24. thread_1.submit_call('func', [1, 2, 3])
  25. thread_2.submit_call('func', [4, 5, 6])
  26. while not thread_1.is_done or not thread_2.is_done:
  27. print("Waiting for threads to finish...")
  28. time.sleep(1)
  29. print("Thread 1 last return value:", thread_1.last_retval())
  30. print("Thread 2 last return value:", thread_2.last_retval())