我的rpi上有一个Python脚本问题。如果我创建一个流程对象,它会自动启动并阻止其他所有操作。我希望它在后台运行,并且能够通过调用start()
方法启动它。
network_manager.py:
import socketserver
class NetworkManagerHandler(socketserver.StreamRequestHandler):
def handle(self):
print("Got some Data!")
class NetworkManagerServer(socketserver.ForkingMixIn, socketserver.TCPServer):
pass
core.py:
import multiprocessing
from network_manager import NetworkManagerServer, NetworkManagerHandler
HOST, PORT = "100.0.0.1", 11891
network_manager = NetworkManagerServer((HOST, PORT), NetworkManagerHandler)
network_manager_process =
multiprocessing.Process(target=network_manager.serve_forever())
# !-> Program is blocking here, but the Server is working. <-!
network_manager_process.daemon = True
network_manager_process.start()
print("Networkmanager is running. (%s:%s)" % (HOST, PORT))
# network_manager.shutdown()
感谢。
答案 0 :(得分:3)
此:
network_manager_process =
multiprocessing.Process(target=network_manager.serve_forever())
应该是这样的:
network_manager_process =
multiprocessing.Process(target=network_manager.serve_forever)
您实际上并不想调用serve_forever
,只是想将该函数传递给Process
对象。