简体   繁体   English

管理多个Twisted客户端连接

[英]Managing multiple Twisted client connections

I'm trying to use Twisted in a sort of spidering program that manages multiple client connections. 我正在尝试在一种管理多个客户端连接的蜘蛛程序中使用Twisted。 I'd like to maintain of a pool of about 5 clients working at one time. 我想维护一个大约5个客户同时工作的池。 The functionality of each client is to connect to a specified IRC server that it gets from a list, enter a specific channel, and then save the list of the users in that channel to a database. 每个客户端的功能是连接到从列表中获取的指定IRC服务器,输入特定通道,然后将该通道中的用户列表保存到数据库。

The problem I'm having is more architectural than anything. 我遇到的问题比任何东西都更具建筑性。 I'm fairly new to Twisted and I don't know what options are available for managing multiple clients. 我是Twisted的新手,我不知道有哪些选项可用于管理多个客户端。 I'm assuming the easiest way is to simply have each ClientCreator instance die off once it's completed its work and have a central loop that can check to see if there's room to add a new client. 我假设最简单的方法是让每个ClientCreator实例在完成其工作后死掉,并有一个中央循环,可以检查是否有空间添加新客户端。 I would think this isn't a particularly unusual problem so I'm hoping to glean some information from other peoples' experiences. 我认为这不是一个特别不寻常的问题,所以我希望从其他人的经历中收集一些信息。

The best option is really just to do the obvious thing here. 最好的选择就是在这里做明显的事情。 Don't have a loop, or a repeating timed call; 没有循环或重复定时呼叫; just have handlers that do the right thing. 只是让处理程序做正确的事情。

Keep a central connection-management object around, and make event-handling methods feed it the information it needs to keep going. 保持一个中央连接管理对象,并使事件处理方法为其提供继续运行所需的信息。 When it starts, make 5 outgoing connections. 启动时,进行5个传出连接。 Keep track of how many are in progress, maintain a list with them in it. 跟踪正在进行的数量,并在其中维护一个列表。 When a connection succeeds (in connectionMade ) update the list to remember the connection's new state. 当连接成功时(在connectionMade )更新列表以记住连接的新状态。 When a connection completes (in connectionLost ) tell the connection manager; 连接完成时(在connectionLost )告诉连接管理器; its response should be to remove that connection and make a new connection somewhere else. 它的响应应该是删除该连接并在其他地方建立新连接。 In the middle, it should be fairly obvious how to fire off a request for the names you need and stuff them into a database (waiting for the database insert to complete before dropping your IRC connection, most likely, by waiting for the Deferred to come back from adbapi ). 在中间,如何触发对所需名称的请求并将它们填充到数据库中是相当明显的(等待数据库插入在放弃IRC连接之前完成,最有可能的是,等待Deferred到来从adbapi回来)。

由于您的每个客户都需要更新数据库,本能地我认为我会捎带connection pool - 请参阅此处了解更多信息(整个文档建议用于使用扭曲时经常出现的一些重要设计模式)。

我不知道你是否被迫使用Twisted,否则你可能想试试Gevent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM