有高手能给我写个关于多线程的python程序吗?

2025-05-14 09:32:21
推荐回答(1个)
回答(1):

#coding=utf-8
import threading
from time import ctime,sleep

res = []
def add(list_num):
    sleep(2)
    re = map(lambda x:x+1,list_num)
    print re
    return re

if __name__ == '__main__':
    threads = []
    for i in range(1,100):
        list_num = []
        if len(list_num)==30:
            t = threading.Thread(target=add,args=(list_num,))
            threads.append(t)
            list_num=[]
        else:
            list_num.append(i)
    if list_num:
        t = threading.Thread(target=add,args=(list_num,))
        threads.append(t)
    for t in threads:
        t.setDaemon(True)
        t.start()
        
    #1~~100 100个数 分为4块
    1--30
    31--60
    61--90
    91--100
    4个线程同时进行 数字的+1操作