zoukankan      html  css  js  c++  java
  • python 多线程小练习

    需求:有100个数据,启动5个线程,每个线程分20个数据,怎么把这20个数据分别传给每个线程。

    1. 利用多线程实现

    import threading
    nums = list(range(100))
    def p(nums):
        for num in numss:
            print(num)
    threads = []
    for i in range(5):
        # nums[0:20] #每次取的值
        # nums[20:40]
        # nums[40:60]
        # nums[60:80]
        # nums[80:]
        t = threading.Thread(target=p,args=(numss[i*20:(i+1)*20],))
        t.start()

    2. 利用线程池实现

    import threadpool
    def p(num):
        print(num)
    res = list(range(100))
    pool = threadpool.ThreadPool(20)#20个线程
    reqs = threadpool.makeRequests(p,res)#生成线程要执行的所有线程,第一个参数为函数名,第二个参数是传的值
    [pool.putRequest(req) for req in reqs] #列表生成式
    pool.wait()#等待 其他线程执行结束
  • 相关阅读:
    hibernate入门
    struts文件上传
    Struts的增删改查
    struts入门
    Maven配置以及环境搭配
    layui增删改查
    easyui三
    A
    C. Permutation Cycle
    E
  • 原文地址:https://www.cnblogs.com/nancyzhu/p/8553360.html
Copyright © 2011-2022 走看看