98_thread.py 713 B

123456789101112131415161718192021222324252627282930
  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. ''')
  15. thread_1.join()
  16. thread_2.join()
  17. thread_1.call('func', [1, 2, 3])
  18. thread_2.call('func', [4, 5, 6])
  19. while not thread_1.is_done or not thread_2.is_done:
  20. print("Waiting for threads to finish...")
  21. time.sleep(1)
  22. print("Thread 1 last return value:", thread_1.last_retval())
  23. print("Thread 2 last return value:", thread_2.last_retval())