python怎样批量生成函数?

xlrd 发布于 2016/05/21 17:11
阅读 1K+
收藏 0

代码:

def main_function(a):
    if a%3==0:
        print a
def t1():
    for a in range(0,100):
        main_function(a)
def t2():
    for a in range(100,200):
        main_function(a)
def t3():
def t4():
.......
pool=ThreadPool(20)
pool.spawn(t1)
pool.spawn(t2)
......

就是类似于这样的,怎样批量生产t1,t2,t3,t4...等。或者说怎样简便的把t函数加入线程池。。

或者像gevent.joinall([func_list])这种形式也行

另外对于multiprocessing,也是面临相同的情况。。怎样简便的多开进程

加载中
2
chengym
chengym
def t(start, end):
    for a in range(start,end):

        main_function(a)

maxnum = 10000000000000
start = 0
for end in range(100, maxnum,100):
   pool.spawn(functools.partial(t, start,end))
   start = end





0
一碗粥
一碗粥
#! /usr/bin/env python
# __*__ coding:utf8 __*__

res = [0, 100, 200, 300]

def main_function(a):
    if a%3 == 0:
        return a
    
ts = [lambda x=res[i],y=res[i+1]:[main_function(a) for a in range(x,y) if main_function(a) != None] for i,v in enumerate(res) if i<len(res)-1]

for t in ts:
    print t()



返回顶部
顶部