1

I try to use below code to let the test1 func thread exit itself after 2 seconds,but find the timeout argument in t.join(timeout=2) doesn't work with test1 function's input() call.

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)

In upon code,after 2 seconds,I input some words to sys.stdin,but it shows me "test1 func runs",that's to say,this line t.join(timeout=2) doesn't work. Can you help me? How can I make the test1 function thread [with input() call] exit itself after 2 seconds?

quanyechavs huo
  • 125
  • 1
  • 13

0 Answers0