有没有办法让 BaseRequestHandler 类有状态?
简短问题
使用下面的示例,是否有一种 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过服务器实例传递对象:
文档 表明您可以通过
handle()
作为self.server
访问服务器实例。You could pass the object through the server instance:
The documentation indicates that you can have access to the server instance in
handle()
asself.server
.