python-如何获取Timer中使用的函数的输出

use*_*ser 5 python timer

我想运行10秒的功能,然后做其他的事情.这是我使用Timer的代码

from threading import Timer
import time

def timeout():
    b='true'
    return b

a='false'    
t = Timer(10,timeout)
t.start()

while(a=='false'):
    print '1'
    time.sleep(1)
print '2'
Run Code Online (Sandbox Code Playgroud)

我知道使用Timer I可以在定时器的末尾打印一些东西(打印b而不是返回b在10秒后返回true).我想知道的是:我可以在"a"中获取timeout()返回的值来正确执行我的while循环吗?

或者是否有另一种方法与另一个功能一起做?

ber*_*eal 5

正如我们在源代码Timer中看到的那样,函数的返回值只是下降了。解决这个问题的一种方法是传递一个可变参数并在函数内改变它:

def work(container):
    container[0] = True

a = [False]
t = Timer(10, work, args=(a,))
t.start()

while not a[0]:
    print "Waiting, a[0]={0}...".format(a[0])
    time.sleep(1)

print "Done, result: {0}.".format(a[0])
Run Code Online (Sandbox Code Playgroud)

或者,使用global,但这不是绅士的行事方式。