调用了返回多个值的外部函数。
def get_name(full_name):
# you code
return first_name, last_name
在简单的函数调用中,我可以获得结果。
from names import get_name
first, last= get_name(full_name)
但我需要使用线程进行调用来获取第一个和最后一个变量的结果值。我未能使用简单的线程调用。
first, last= Threading.thread(get_name, args= (full_name,)
请帮我获取函数调用的返回值
你应该使用queue https://docs.python.org/3/library/queue.html为了从线程检索数据,这里有一个使用包装器将函数中的值存储到队列中的示例:
import threading
import queue
my_queue = queue.Queue()
def storeInQueue(f):
def wrapper(*args):
my_queue.put(f(*args))
return wrapper
@storeInQueue
def get_name(full_name):
return full_name, full_name
t = threading.Thread(target=get_name, args = ("foo", ))
t.start()
my_data = my_queue.get()
print(my_data)
在这里你有现场工作示例 https://repl.it/repls/DigitalGrimyPackage
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)