当前位置: 首页 > news >正文

python 进程

1创建一个爬虫程序

import requests
urls = [
    'https://www.cnblogs.com/#p{page}'
    for page in range(1, 50+1)
]

def craw(url):
    r = requests.get(url)
    print(url, len(r.text))

craw(urls[0])

2定义单进程和多进程

import blob_spider
import threading
import time

def single_thread():
    print("singe_thread begin")
    for url in blob_spider.urls:
        blob_spider.craw(url)
    print("singe_thread end")


def multi_thread():
    print("multi_thread begin")
    threads = []
    for url in blob_spider.urls:
        threads.append(
            threading.Thread(target=blob_spider.craw, args=(url, )) # 创建线程
        )

    for thread in threads:
        thread.start()

    for thread in threads:
        thread.join()
    print("multi_thread end")

if __name__ == "__main__":
    start = time.time()
    single_thread()
    end = time.time()
    print("single_thread用时:{}".format(end-start))
    start = time.time()
    multi_thread()
    end = time.time()
    print("multi_thread用时:{}".format(end - start))
http://www.dtcms.com/a/7758.html

相关文章:

  • 附1:k8s服务器初始化
  • c语言小游戏之扫雷
  • Codewave学习体验分享:低代码开发世界的黑马
  • 分享7种SQL的进阶用法
  • Go 的 Http 请求系统指南
  • 百度Apollo | 实车自动驾驶:感知、决策、执行的无缝融合
  • 【数学建模】插值与拟合
  • 旅游MR混合现实情景实训教学系统教学
  • Selenium 自动化截取网页指定区域截图
  • JS 中的 async 与 await
  • Git基础
  • uniapp开发过程一些小坑
  • 认识数学建模
  • linux bash shell的getopt以及函数用法小记
  • 携程基于Jira Cloud的敏捷项目管理实践
  • EXCEL VBA获取幸运数字号码
  • Sentinel降级操作
  • 数据预处理技术之数据归一化
  • k8s中netty服务器容器tcp连接数量优化
  • Sentinel-1 扩展时序注释数据集 (ETAD)的查询和下载
  • Android 基于Fragment的权限封装
  • ASP.NET Core NE8实现HTTP Upgrade和HTTP CONNECT代理服务器
  • php/js:实现几秒后进行页面跳转
  • 【软考中级】3天擦线过软考中级-软件设计师
  • 使用vscode查bug
  • 几款提高开发效率的Idea 插件
  • 力扣题目训练(1)
  • c++ class总结
  • 微认证 openEuler社区开源贡献实践
  • 头歌C语言递归函数、嵌套函数