如何在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方法时,此时会调用该方法,因此无限循环和请求永远不会返回