python threadpool有多个参数怎么提交
3个回答
展开全部
仔细的研究了一下,发现函数调用时
result = request.callable(*request.args, **request.kwds)
第一个解包list,第二个解包dict,所以可以这样:
#----------------------------------------------------------------------
def hello(m, n, o):
""""""
print "m = %s, n = %s, o = %s"%(m, n, o)
if __name__ == '__main__':
# 方法1
lst_vars_1 = ['1', '2', '3']
lst_vars_2 = ['4', '5', '6']
func_var = [(lst_vars_1, None), (lst_vars_2, None)]
# 方法2
dict_vars_1 = {'m':'1', 'n':'2', 'o':'3'}
dict_vars_2 = {'m':'4', 'n':'5', 'o':'6'}
func_var = [(None, dict_vars_1), (None, dict_vars_2)]
pool = threadpool.ThreadPool(2)
requests = threadpool.makeRequests(hello, func_var)
[pool.putRequest(req) for req in requests]
pool.wait()
result = request.callable(*request.args, **request.kwds)
第一个解包list,第二个解包dict,所以可以这样:
#----------------------------------------------------------------------
def hello(m, n, o):
""""""
print "m = %s, n = %s, o = %s"%(m, n, o)
if __name__ == '__main__':
# 方法1
lst_vars_1 = ['1', '2', '3']
lst_vars_2 = ['4', '5', '6']
func_var = [(lst_vars_1, None), (lst_vars_2, None)]
# 方法2
dict_vars_1 = {'m':'1', 'n':'2', 'o':'3'}
dict_vars_2 = {'m':'4', 'n':'5', 'o':'6'}
func_var = [(None, dict_vars_1), (None, dict_vars_2)]
pool = threadpool.ThreadPool(2)
requests = threadpool.makeRequests(hello, func_var)
[pool.putRequest(req) for req in requests]
pool.wait()
展开全部
抄来抄去,只对当前案例有效,callback参数不需要写都可以,不必要画蛇添足,加None,这是我的代码,threadpool和multiprocessing的区别在于,zip会造成传入值的错乱,默认前zip单个list中,两个为一个线程的参数,自己慢慢体会吧。 for attr in attrs:
# if attr in notdealed:
# source_url.append(source_addr + attr)
# dire_url.append(dire_addr + attr)#attr[0: attr.index('.')] + r"_ID.txt")
source_url = []
source_url.append(source_addr + attr)
source_url.append(dire_addr + attr)#attr[0: attr.index('.')] + r"_ID.txt")
# print(source_url)
# func.append((source_url,None))
func.append(source_url)
# print(func)
# exit()
"""多线程编程"""
pool = tp.ThreadPool(len(attrs))
requests = tp.makeRequests(job,func)
[pool.putRequest(req) for req in requests]
pool.wait()
# if attr in notdealed:
# source_url.append(source_addr + attr)
# dire_url.append(dire_addr + attr)#attr[0: attr.index('.')] + r"_ID.txt")
source_url = []
source_url.append(source_addr + attr)
source_url.append(dire_addr + attr)#attr[0: attr.index('.')] + r"_ID.txt")
# print(source_url)
# func.append((source_url,None))
func.append(source_url)
# print(func)
# exit()
"""多线程编程"""
pool = tp.ThreadPool(len(attrs))
requests = tp.makeRequests(job,func)
[pool.putRequest(req) for req in requests]
pool.wait()
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我是把几个参数做成字典传递的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询