当Python线程完成时如何通知

huangapple go评论117阅读模式
英文:

How to notify when python thread finished

问题

当线程完成时,我想调用回调函数。而且它应该是线程安全的。

我想要一个解决方案,可以在线程完成时调用回调函数,而且它应该是线程安全的。

  1. my_thread = threading.Thread(target=do_work)
  2. my_thread.finished.connect(thread_finished)
  3. my_thread.start()
英文:

I want to call callback function when thread finished. And it should be thread safe

I want solution to call a callback function when thread finished and it should be thread safe.

  1. my_thread = threading.Thread(target=do_work)
  2. my_thread.finished.connect(thread_finished)
  3. my_thread.start()

答案1

得分: 0

你最好的选择是期货。

  1. from concurrent.futures import ThreadPoolExecutor
  2. from time import sleep
  3. def do_work():
  4. sleep(2)
  5. return 10
  6. def im_done(future):
  7. print("未来的结果是", future.result())
  8. with ThreadPoolExecutor() as executor:
  9. future = executor.submit(do_work)
  10. future.add_done_callback(im_done)
英文:

You best bet is futures.

  1. from concurrent.futures import ThreadPoolExecutor
  2. from time import sleep
  3. def do_work():
  4. sleep(2)
  5. return 10
  6. def im_done(future):
  7. print("Result of future is", future.result())
  8. with ThreadPoolExecutor() as executor:
  9. future = executor.submit(do_work)
  10. future.add_done_callback(im_done)

答案2

得分: -1

你可以使用这个对我有用的解决方案。
而且它是线程安全的。

  1. import time
  2. from threading import Thread
  3. from pyrvsignal import Signal
  4. class MyThread(Thread):
  5. started = Signal()
  6. finished = Signal()
  7. def __init__(self, target, args):
  8. self.target = target
  9. self.args = args
  10. Thread.__init__(self)
  11. def run(self) -> None:
  12. self.started.emit()
  13. self.target(*self.args)
  14. self.finished.emit()
  15. def do_my_work(details):
  16. print(f"Doing work: {details}")
  17. time.sleep(10)
  18. def started_work():
  19. print("Started work")
  20. def finished_work():
  21. print("Work finished")
  22. thread = MyThread(target=do_my_work, args=("testing",))
  23. thread.started.connect(started_work)
  24. thread.finished.connect(finished_work)
  25. thread.start()

参考此链接 - 通知线程事件

英文:

You can use this solution worked for me.
And it is thread safe.

  1. import time
  2. from threading import Thread
  3. from pyrvsignal import Signal
  4. class MyThread(Thread):
  5. started = Signal()
  6. finished = Signal()
  7. def __init__(self, target, args):
  8. self.target = target
  9. self.args = args
  10. Thread.__init__(self)
  11. def run(self) -> None:
  12. self.started.emit()
  13. self.target(*self.args)
  14. self.finished.emit()
  15. def do_my_work(details):
  16. print(f"Doing work: {details}")
  17. time.sleep(10)
  18. def started_work():
  19. print("Started work")
  20. def finished_work():
  21. print("Work finished")
  22. thread = MyThread(target=do_my_work, args=("testing",))
  23. thread.started.connect(started_work)
  24. thread.finished.connect(finished_work)
  25. thread.start()

Refer this - Notify threading events

huangapple
  • 本文由 发表于 2023年8月10日 09:24:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76872114.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定