1.多线程网络IO请求:

#!/usr/bin/python
#coding:utf-8

from concurrent.futures import ThreadPoolExecutor
import requests
#线程池
# def get_page(url):
#     response = requests.get(url)
#     print response.url
#     return response
#
# urllist=["https://www.baidu.com/","https://www.jianshu.com/","https://i.cnblogs.com/"]
# pool = ThreadPoolExecutor(5) #最多能运行5个线程
# for url in urllist:
#     pool.submit(get_page,url)  #将线程(函数和参数)提交到线程池中
# pool.shutdownn(wait=True)


#线程池加回调函数
def get_page(url):
    response = requests.get(url)
    print response.url
    return response
def callback(future):
    print future.result
urllist=["https://www.baidu.com/","https://www.jianshu.com/","https://i.cnblogs.com/"]
pool = ThreadPoolExecutor(5) #最多能运行5个线程
for url in urllist:
    future = pool.submit(get_page,url)  #将线程(函数和参数)提交到线程池中,返回Future对象
    future.add_done_callback(callback)
pool.shutdownn(wait=True)
线程池加回调函数

相关文章:

  • 2021-08-14
  • 2022-01-23
  • 2021-06-27
  • 2021-04-11
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-02
  • 2021-10-03
  • 2021-05-28
  • 2021-08-27
  • 2021-05-27
相关资源
相似解决方案