我现在在用 socketserver.server_forever()处理请求,在 handle ()里面接受数据存入 queue,现在想用另外个线程取出 queue 数据并存入数据库,想问这个新线程在哪里开启呢?发现 server_forever 一直运行,新线程启动不了 if name == "main": HOST, PORT = "0.0.0.0", 9999 # Linux server = socketserver.ThreadingTCPServer((HOST, PORT), MyTCPHandler) # 线程 server.serve_forever() connect_sql() init_db() for i in range(multiprocessing.cpu_count()): t = threading.Thread(target=getsensor_que) t.start()
1
zy820 OP if __name__ == "__main__":
sensor_que = queue.Queue() sensor_data = {'DeviceId': '', 'AirPressure': 0, 'Humidity': 0, 'Noise': 0, 'Pm25': 0, 'Temperature': 0, 'WindDirection': 0, 'WindSpeed': 0} # HOST, PORT = "localhost", 9999 #windows HOST, PORT = "0.0.0.0", 9999 # Linux server = socketserver.ThreadingTCPServer((HOST, PORT), MyTCPHandler) # 线程 server.serve_forever() print('before init_db!') connect_sql() init_db() print('after init_db!') for i in range(multiprocessing.cpu_count()): t = threading.Thread(target=getsensor_que) t.start() |