从线程返回值 [英] Return value from thread
本文介绍了从线程返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Python中,如何获取线程以将元组或我选择的任何值返回给父级?
How do I get a thread to return a tuple or any value of my choice back to the parent in Python?
推荐答案
I suggest you instantiate a Queue.Queue before starting the thread, and pass it as one of the thread's args: before the thread finishes, it .put
s the result on the queue it received as an argument. The parent can .get
or .get_nowait
it at will.
队列通常是在Python中安排线程同步和通信的最佳方法:队列本质上是线程安全的消息传递工具,通常是组织多任务的最佳方法!-)
Queues are generally the best way to arrange thread synchronization and communication in Python: they're intrinsically thread-safe, message-passing vehicles -- the best way to organize multitasking in general!-)
这篇关于从线程返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文