当前位置:首页 > Python教程 > python技巧

python 多线程(多个线程一起返回结果)

待整理:

测试代码:

            ‘‘‘

            Created on 2020年7月21日

@author: sea

            ‘‘‘

            from
             concurrent.futures.thread import ThreadPoolExecutor

            from
             concurrent.futures._base import as_completed

            from
             time import sleep


def send_request(req_url,json):
#     print(req_url+ ""+json)
    sleep(3)
    return req_url
    
    
    
if __name__ == __main__:
    executor = ThreadPoolExecutor(max_workers=3)
    all_task=[]
    for i in range(10):
        pass
        task = executor.submit(send_request,"url--"+ str(i),str(i)+"json")
        all_task.append(task)
        print("all_task size is "+str(len(all_task)))
    for future in as_completed(all_task):
        pass
        result = future.result()
        print(result)

 

原文:https://www.cnblogs.com/lshan/p/13356788.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!