为每台服务器分配相等数量的关键字列表

 1 def kw_to_ervery_server(kw,server_total,server_num=1):
 2     '''
 3         @kw 是数据库读取的关键字列表
 4         @server_total 服务器台数
 5         @server_num 是服务器编号第一台服务器编号是1,第二台就是2,依次类推
 6         @注意:server_num  < server_total and server_num  > 0
 7         
 8     '''
 9     if server_num == 0:
10         server_num = 1
11     if (server_num <= 0) or (server_num > server_total):
12         print 'server_num must be < server_total or server_num must > zero'
13         return
14     len_kw = len(kw) #关键词
15     num_list = []
16     n1 = int(len_kw/server_total)
17     i = 0
18     n_next = 0
19     while i <= server_total:
20         if i == server_total:
21             n_next = len_kw
22         num_list.append(n_next)
23         n_next += n1
24         i += 1 
25     kw_list=kw[num_list[server_num-1]:num_list[server_num]] #每台服务器的关键字列表
26 
27     return kw_list
28 
29 
30 if __name__ == '__main__':
31     kw = range(110)
32     server_total = 10
33     server_num = 0
34     kw = kw_to_ervery_server(kw,server_total)
35     print kw

 

posted @ 2016-10-26 23:23  恰似少年  阅读(148)  评论(0编辑  收藏  举报