我在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()


谢谢。

最佳答案

这个:

network_manager_process =
            multiprocessing.Process(target=network_manager.serve_forever())


应该是这样的:

network_manager_process =
            multiprocessing.Process(target=network_manager.serve_forever)


您实际上并不想调用serve_forever,只想将函数传递给Process对象。

10-08 14:22