使用输入函数调用

时间:2017-10-26 02:15:37

标签: multithreading python-3.x

我尝试使用下面的代码让test1的func线程在2秒后退出,但是在t.join(timeout = 2)中找到timeout参数并不适用于test1函数的输入( )打电话。

import threading


class MyThread(threading.Thread):

    def __init__(self, func, args, name=''):
        threading.Thread.__init__(self)
        self.name = name
        self.func = func
        self.args = args

    def run(self):
        self.result = self.func(*self.args)

    def get_result():
        return self.result


def test1():
    a = input()
    print("test1 func runs")


t = MyThread(test1, ())
t.setDaemon(True)
t.start()
t.join(timeout=2)
while 1:
    import time
    time.sleep(1)

在代码中,在2秒之后,我向sys.stdin输入了一些单词,但它显示我" test1 func运行",这就是说,这一行t.join(timeout=2)不起作用。你能帮助我吗?如何让test1函数thread [with input()call]在2秒后退出?

0 个答案:

没有答案