如何在python中在后台运行线程

如何在python中在后台运行线程,python,multithreading,python-multithreading,Python,Multithreading,Python Multithreading,类CORSRequestHandler(SimpleHTTPRequestHandler): def do_获得(自我): thread1=threading.Thread(目标=test()) thread1.daemon=True thread1.start() 返回SimpleHTTPRequestHandler.do\u GET(self) def test(): 尽管如此: 打印“你好” 时间。睡眠(2) 如果uuuu name uuuuuu='\uuuuuuu main\uuuuuu

类CORSRequestHandler(SimpleHTTPRequestHandler):
def do_获得(自我):
thread1=threading.Thread(目标=test())
thread1.daemon=True
thread1.start()
返回SimpleHTTPRequestHandler.do\u GET(self)
def test():
尽管如此:
打印“你好”
时间。睡眠(2)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':

BaseHTTPServer.test(CORSRequestHandler,BaseHTTPServer.HTTPServer)
您需要通过
threading.Thread的
关键字参数中的方法
测试
,而不是
测试
返回的内容。
那么,替换

thread1 = threading.Thread(target=test())


当您执行
target=test()
时,此时会调用test方法,因此无限循环和请求永远不会返回。

您需要通过
target
中的
test
关键字参数
threading.Thread
,而不是
test
返回的内容。 那么,替换

thread1 = threading.Thread(target=test())

当您执行
target=test()
test方法时,此时会调用该方法,因此无限循环和请求永远不会返回