有没有办法让 BaseRequestHandler 类有状态?

发布于 2024-12-21 23:44:12 字数 1689 浏览 4 评论 0原文

简短问题
使用下面的示例,是否有一种 Pythonic 方法可以与 BaseRequestHandler 类共享 my_object 的实际实例?

背景
根据定义,BaseRequestHandler 类为每个请求创建一个新实例。因此,我正在努力寻找如何将数据从 handle() 函数返回到 ProtocolInterface 实例的解决方案。请注意,如果我需要在 handle() 中执行除打印到标准输出之外的操作,这可能是错误的方法。

此时,我不相信全局变量会起作用,因为 my_object 被传入并且预计会经常更改(这就是为什么 handle() 需要查看要查看示例客户端(发送虚假数据),请参阅我的其他问题。是套接字服务器在后台线程中运行

我想要做的示例

class ProtocolHandler(SocketServer.BaseRequestHandler):
    def handle(self):
        while(1):
            self.data = self.request.recv(1024)
            if self.data == '':
                break
            self.request.send("Success") 
            self.my_object.success = True#  <--- How can I share my_object's instance?

class ProtocolInterface():
    def __init__(self, obj, host='127.0.0.1', port=8000, single_connection=False):
        self.my_object = obj # <--- This ideally is the same instance seen in ProtocolHandler
        self.host = host
        self.port = port

        # Create the socket server to process in coming traffic
        if(single_connection):
            self.server = SocketServer.TCPServer((self.host, self.port), ProtocolHandler)
        else:
            self.server = SocketServer.ThreadingTCPServer((self.host, self.port), ProtocolHandler)

    def start(self):
        print "Server Starting on HOST: " + self.host
        server_thread = threading.Thread(target=self.server.serve_forever)
        server_thread.daemon = True
        server_thread.start()

Short Question
Using my examples below, is there a Pythonic way to share my_object's actual instance with with the BaseRequestHandler class?

Background
By definition, the BaseRequestHandler class creates a new instance for each request. Because of this, I am struggling to try find a solution on how to get data from the handle() function back to the ProtocolInterface instance. Note that this might be the wrong approach if I am needing to do something in handle() other than print to stdout.

At this point in time, I do not believe that global variables will work because my_object is passed in and is expected to change often (this is why handle() needs to see it. To see an example client (sending bogus data) see my other SO question. I think the biggest issue I am facing is the the socketservers are running in a background thread.

Example of what I would like to do

class ProtocolHandler(SocketServer.BaseRequestHandler):
    def handle(self):
        while(1):
            self.data = self.request.recv(1024)
            if self.data == '':
                break
            self.request.send("Success") 
            self.my_object.success = True#  <--- How can I share my_object's instance?

class ProtocolInterface():
    def __init__(self, obj, host='127.0.0.1', port=8000, single_connection=False):
        self.my_object = obj # <--- This ideally is the same instance seen in ProtocolHandler
        self.host = host
        self.port = port

        # Create the socket server to process in coming traffic
        if(single_connection):
            self.server = SocketServer.TCPServer((self.host, self.port), ProtocolHandler)
        else:
            self.server = SocketServer.ThreadingTCPServer((self.host, self.port), ProtocolHandler)

    def start(self):
        print "Server Starting on HOST: " + self.host
        server_thread = threading.Thread(target=self.server.serve_forever)
        server_thread.daemon = True
        server_thread.start()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

提赋 2024-12-28 23:44:12

您可以通过服务器实例传递对象:

self.server = SocketServer.TCPServer((self.host, self.port), ProtocolHandler)
self.server.my_object = self.my_object

文档 表明您可以通过 handle() 作为 self.server 访问服务器实例。

You could pass the object through the server instance:

self.server = SocketServer.TCPServer((self.host, self.port), ProtocolHandler)
self.server.my_object = self.my_object

The documentation indicates that you can have access to the server instance in handle() as self.server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文