1
0

98_thread.py 806 B

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