zoukankan      html  css  js  c++  java
  • python threadpool使用注意事项

    常见的使用方法如下:

    #!/usr/bin/env python
    import threadpool
    import time,random
     
    def hello(str):
        time.sleep(2)
        return str
     
    def print_result(request, result):
        print "the result is %s %r" % (request.requestID, result)
     
    data = [random.randint(1,10) for i in range(20)]
     
    pool = threadpool.ThreadPool(5)
    requests = threadpool.makeRequests(hello, data, print_result)
    [pool.putRequest(req) for req in requests]
    pool.wait() 

    如果hello函数有多个参数,那就需要注意下了,这时你就需要使用字典列表

    比如如果你有两个参数 ip,port,那么这个data就应该是这种形式:

    data=[]

    data.append({‘ip’:'***','port':12345})

    之前尝试使用下面这种形式,一直报错

    data=[]

    data.append(('***',12345))

    后来仔细看了api文档,终于找到解决办法

    以后遇到问题还是好好看看官方API文档来得快啊

  • 相关阅读:
    Python 列表浅拷贝与深拷贝
    Linux 基本命令-----常用操作分类
    硬盘的分区方式
    github中fork的使用
    大O记号
    python的__file__和__name__变量
    python生成器
    python装饰器
    re模块元字符
    python_数据类型_list
  • 原文地址:https://www.cnblogs.com/chinasun021/p/2972966.html
Copyright © 2011-2022 走看看