zoukankan      html  css  js  c++  java
  • 关于python使用threadpool中的函数单个参数和多个参数用法举例

    1.对单个元素的函数使用线程池:

    # encoding:utf-8
    __author__='xijun.gong'
    import threadpool
    
    
    def func(name):
        print 'hi {}
    '.format(name)
    
    if __name__ == '__main__':
        data = ['xijun.gong', 'xijun', 'gxjun']
        pool = threadpool.ThreadPool(5)
        reqs = threadpool.makeRequests(func, data)
        [pool.putRequest(req) for req in reqs]
        pool.wait()

    结果:

    hi xijun.gong

    hi xijun

    hi gxjun

    2.对于多个参数的情况使用方式:

    # encoding:utf-8
    __author__='xijun.gong'
    import threadpool
    
    
    def func(name):
        print 'hi {}
    '.format(name)
    
    def add(a,b):
        print '{0}+{1}={2}'.format(a,b,(a+b))
    if __name__ == '__main__':
        data = [((index,i),None) for index,i in enumerate(range(1,10,2))]#(index,i)也可以写成[index,i]
        pool = threadpool.ThreadPool(5)
        reqs = threadpool.makeRequests(add, data)
        [pool.putRequest(req) for req in reqs]
        pool.wait()

    结果:

    0+1=1
    1+3=4
    3+7=10
    2+5=7
    4+9=13

    3.如果我们想不安参数顺序赋值,可以使用这种方式:

    # encoding:utf-8
    __author__='xijun.gong'
    import threadpool
    
    
    def func(name):
        print 'hi {}
    '.format(name)
    
    def add(a,b):
        print '{0}+{1}={2}'.format(a,b,(a+b))
    if __name__ == '__main__':
        data = [(None,{'b':index,'a':i}) for index,i in enumerate(range(1,10,2))]
        pool = threadpool.ThreadPool(5)
        reqs = threadpool.makeRequests(add, data)
        [pool.putRequest(req) for req in reqs]
        pool.wait()

    结果:

    1+0=1
    3+1=4
    5+2=7
    7+3=10
    9+4=13

  • 相关阅读:
    .net core 灵活读取配置文件
    SUSE12SP3-Mysql5.7安装
    SUSE12Sp3-MongoDB安装
    SUSE12Sp3-Supervisor 守护.net core进程
    SUSE12Sp3-Nginx安装
    SUSE12Sp3-.NET Core 2.2.1 runtime安装
    搭建consul 集群
    SUSE12Sp3安装配置.net core 生产环境(1)-IP,DNS,网关,SSH,GIT
    使用Consul 实现 MagicOnion(GRpc) 服务注册和发现
    使用MagicOnion实现gRPC
  • 原文地址:https://www.cnblogs.com/gongxijun/p/6862333.html
Copyright © 2011-2022 走看看