【发布时间】:2014-03-30 04:18:00
【问题描述】:
假设您有 1000 个客户端连接到一个扭曲的服务器。这些客户端不断地轮询更新(或者只是通过WebSocket 之类的东西持续连接)。服务器的工作是遍历所有 1000 个,检查其中哪些有更新,然后在更新时将其发送出去。
有没有办法做到,在 Twisted 中,我可以在 4 个线程中运行 250 个(或者进一步划分它)? Twisted 如何决定一次运行多少个任务?我可以连续检查250,甚至不时更改分布吗?
这是我想象的一个非常简单的版本,虽然我不知道它在 Twisted 中的样子:
class Server:
queues = [["250 clients"], ["250 clients"], ["250 clients"], ["250 clients"]]
@classmethod
def run_server(cls):
for i in xrange(len(cls.queues)):
# this would run in its own thread
cls.continuously_check_for_updates(cls.queues[i])
# server mainloop
while True:
if get_client_connection():
store_client_in_queue()
distribute_client_queues()
@classmethod
def continuously_check_for_updates(cls, queue):
i = 0
while True:
if i >= len(queue):
i = 0
client = queue[i]
if check_for_updates(client):
report_update(client)
i += 1
感谢您的洞察力和建议。谢谢。
【问题讨论】:
标签: python multithreading websocket twisted