Python threadpool多参数情况

不屈意志 2016-01-04 09:51:50
如果被执行的函数 有多个参数 怎么写呢?

def hello(str, n, m):
print str
time.sleep(3)
return n

这个函数,如何设置其参数呢?threadpool.makeRequests(hello, 参数列表, print_result)
官方文档对参数的解释没看懂:args_list contains the parameters for each invocation of callable. Each item in args_list should be either a 2-item tuple of the list of positional arguments and a dictionary of keyword arguments or a single, non-tuple argument.
...全文
1285 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
现在ing 2016-01-07
  • 打赏
  • 举报
回复
引用 2 楼 wxl2578 的回复:
快来个人回复一下,把可用分赚了
解决了就好
不屈意志 2016-01-07
  • 打赏
  • 举报
回复
快来个人回复一下,把可用分赚了
不屈意志 2016-01-06
  • 打赏
  • 举报
回复
仔细的研究了一下,发现函数调用时
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()        

37,722

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧