Python/Twisted - 发送到特定的套接字对象?
我在一个节点上有一个“管理器”进程,以及几个工作进程。 管理器是实际的服务器,它拥有与客户端的所有连接。 管理器接受所有传入的数据包并将它们放入队列中,然后工作进程将数据包从队列中拉出,处理它们并生成结果。 他们将结果发送回管理器(通过将它们放入管理器读取的另一个队列中),但这就是我陷入困境的地方:如何将结果发送到特定的套接字? 在单个进程上处理数据包时,这很容易,因为当您收到数据包时,您可以通过获取上下文中的“传输”对象来回复它。 但我该如何用我正在使用的方法来做到这一点呢?
I have a "manager" process on a node, and several worker processes. The manager is the actual server who holds all of the connections to the clients. The manager accepts all incoming packets and puts them into a queue, and then the worker processes pull the packets out of the queue, process them, and generate a result. They send the result back to the manager (by putting them into another queue which is read by the manager), but here is where I get stuck: how do I send the result to a specific socket? When dealing with the processing of the packets on a single process, it's easy, because when you receive a packet you can reply to it by just grabbing the "transport" object in-context. But how would I do this with the method I'm using?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您可能需要在“事件”对象中保留对传输(或协议)的引用以及刚刚进入该协议的字节。 这样,连接上传入的响应就会在同一连接上发出。
如果不需要串行处理,也许您应该考虑设置可以并行处理数据的函子,以消除排队的需要。 请记住,您需要保护代码的关键部分。
编辑:
从您关于评估服务器设计的其他问题来看,并行处理似乎不可能适合您的情况,所以我的第一个建议成立。
It sounds like you might need to keep a reference to the transport (or protocol) along with the bytes the just came in on that protocol in your 'event' object. That way responses that came in on a connection go out on the same connection.
If things don't need to be processed serially perhaps you should think about setting up functors that can handle the data in parallel to remove the need for queueing. Just keep in mind that you will need to protect critical sections of your code.
Edit:
Judging from your other question about evaluating your server design it would seem that processing in parallel may not be possible for your situation, so my first suggestion stands.