示例 urllib3 和 python 中的线程

2024-04-16

我正在尝试在简单线程中使用 urllib3 来获取多个 wiki 页面。 该脚本将

为每个线程创建 1 个连接(我不明白为什么)并永远挂起。 urllib3 和线程的任何提示、建议或简单示例

import threadpool
from urllib3 import connection_from_url

HTTP_POOL = connection_from_url(url, timeout=10.0, maxsize=10, block=True)

def fetch(url, fiedls):
  kwargs={'retries':6}
  return HTTP_POOL.get_url(url, fields, **kwargs)

pool = threadpool.ThreadPool(5)
requests = threadpool.makeRequests(fetch, iterable)
[pool.putRequest(req) for req in requests]

@Lennart 的脚本出现此错误:

http://en.wikipedia.org/wiki/2010-11_Premier_LeagueTraceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/threadpool.py", line 156, in run
 http://en.wikipedia.org/wiki/List_of_MythBusters_episodeshttp://en.wikipedia.org/wiki/List_of_Top_Gear_episodes http://en.wikipedia.org/wiki/List_of_Unicode_characters    result = request.callable(*request.args, **request.kwds)
  File "crawler.py", line 9, in fetch
    print url, conn.get_url(url)
AttributeError: 'HTTPConnectionPool' object has no attribute 'get_url'
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/threadpool.py", line 156, in run
    result = request.callable(*request.args, **request.kwds)
  File "crawler.py", line 9, in fetch
    print url, conn.get_url(url)
AttributeError: 'HTTPConnectionPool' object has no attribute 'get_url'
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/threadpool.py", line 156, in run
    result = request.callable(*request.args, **request.kwds)
  File "crawler.py", line 9, in fetch
    print url, conn.get_url(url)
AttributeError: 'HTTPConnectionPool' object has no attribute 'get_url'
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/threadpool.py", line 156, in run
    result = request.callable(*request.args, **request.kwds)
  File "crawler.py", line 9, in fetch
    print url, conn.get_url(url)
AttributeError: 'HTTPConnectionPool' object has no attribute 'get_url'

添加后import threadpool; import urllib3 and tpool = threadpool.ThreadPool(4)@user318904 的代码出现此错误:

Traceback (most recent call last):
  File "crawler.py", line 21, in <module>
    tpool.map_async(fetch, urls)
AttributeError: ThreadPool instance has no attribute 'map_async'

这是我的看法,使用 Python3 的更新解决方案和concurrent.futures.ThreadPoolExecutor.

import urllib3
from concurrent.futures import ThreadPoolExecutor

urls = ['http://en.wikipedia.org/wiki/2010-11_Premier_League',
        'http://en.wikipedia.org/wiki/List_of_MythBusters_episodes',
        'http://en.wikipedia.org/wiki/List_of_Top_Gear_episodes',
        'http://en.wikipedia.org/wiki/List_of_Unicode_characters',
        ]

def download(url, cmanager):
    response = cmanager.request('GET', url)
    if response and response.status == 200:
        print("+++++++++ url: " + url)
        print(response.data[:1024])

connection_mgr = urllib3.PoolManager(maxsize=5)
thread_pool = ThreadPoolExecutor(5)
for url in urls:
    thread_pool.submit(download, url, connection_mgr)

一些备注

  • 我的代码基于类似的示例Python Cookbook作者:比兹利和琼斯。
  • 我特别喜欢这样一个事实:除了这个之外,你只需要一个标准模块urllib3.
  • 设置非常简单,如果您只想获得副作用download(如打印、保存到文件等),连接线程不需要额外的工作。
  • 如果你想要一些不一样的东西,ThreadPoolExecutor.submit实际上返回任何内容download会回来,包裹在Future.
  • 我发现将线程池中的线程数与线程数对齐很有帮助HTTPConnection位于连接池中(通过maxsize)。否则,当所有线程尝试访问同一服务器时(如示例中所示),您可能会遇到(无害的)警告。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

示例 urllib3 和 python 中的线程 的相关文章

随机推荐

  • 如何将 NSMutableArray 的元素转换为 NSString

    我有 1 个 NSMutableArray 我想转换数组中的任何数据将在 NSString 中 告诉我代码 Array 只是 NSMutableArray 类的对象 如果您只想要数组的元素 那么您可以尝试 ComponentsJoinedB
  • Eclipse Bug:未处理的事件循环异常没有更多句柄

    我使用 Swing 和 MigLayout 构建了一个 GUI 我在 Windows 7 Ultimate 上使用 Eclipse 4 2 2 64 位 每次我单击返回窗口编辑代码时 都会出现一个弹出窗口 然后提示我重新启动 Eclipse
  • 如何将 YAML 转换为 JSON? [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我正在寻找在 YAML 文件和 JSON 之间进行转换 这真的很难找到任何信息 如果不需要 Json NET 的功能 也可以直接使用 Se
  • 需要:Rikulo UXL 示例

    我想让一些 UXL 与 Dart 编辑器一起使用 我担心我感觉示例代码用户体验实验室概述 http docs rikulo org ui latest UXL Fundamentals UXL Overview html要么已经过时 要么缺
  • 龙卷风错误处理

    我希望能够处理输入错误 URL 时显示的更好的错误E g localhost 8000 AFDADSFDKFADS 我收到一条丑陋的 python 回溯消息 因为抛出了一个ornado web HTTPError 异常 我知道我可以使用正则
  • iOS 9+ 通用链接不适用于 Google 应用

    我工作的公司让我问以下问题 当在其他应用程序 原生 Apple 应用程序 Slack 等 中点击通用链接时 我们的应用程序会打开 但如果在任何 Google 应用程序 环聊 收件箱等 中点击相同链接 则不会直接打开通用链接通过我们的应用程序
  • 在 Visual Studio 2017 中删除垂直虚线缩进线

    我刚刚安装了 Visual Studio 2017 代码编辑器中出现了奇怪的垂直缩进标记 我怎样才能删除它们 附 我禁用了所有扩展 但这没有帮助 下面有一个选项Tools Options Text Editor called Show st
  • mvc3 路由设置为 id, id2 id3

    我有以下区域路线设置 context MapRoute Admin default3 Admin controller action id id2 id3 new action Index context MapRoute Admin de
  • capistrano,:db 角色,它的用途是什么?

    据我所知 卡皮斯特拉诺 db角色仅用于运行迁移 因此 在大多数情况下 它可能不应该实际上是运行数据库的服务器 为什么那里会有 ruby rails 堆栈 或者允许 ssh 登录 它只是您想要实际执行 Rails 迁移的任何服务器 并且只有标
  • 实现移动运动体

    Project 我正在制作一个简单的项目 其中我希望能够制作一个运动体并将其从 x 点移动到 y 点 创建运动体 似乎没有太多直接内容 我一直在关注一些关于实现运动体的不同教程 因为我找不到指定如何正确执行此操作的教程 Problem 这似
  • 如何使用数据列表比较并返回数据

    我是 Haskell 的新手 我正在努力寻找一种使用类成员变量来返回我正在寻找的成员变量的方法 我有这个数据 data Place Place name String north Float east Float rainfall Int
  • 奥尔森 时区 到 windows

    我需要在 Windows 中从奥尔森时区转换时区 使用PHP 我找到了这个http unicode org repos cldr trunk common supplemental windowsZones xml http unicode
  • 用于搜索 Google 云端硬盘的 Google Apps 脚本

    是否可以使用 Google Apps 脚本在 Google 云端硬盘中搜索文档和文件夹 谷歌已经取消了自己的文档 驱动器搜索小工具 因为它似乎依赖于 iGoogle 谷歌企业支持也承认了这一点 谢谢 我想你正在寻找搜索文件 https de
  • Unity使用Invoke在另一个脚本上调用方法

    我有两个脚本 其中一个重新启动场景 另一个是倒计时器 而不是在第一个脚本中调用重新启动场景方法 但是 它没有重新启动 即使没有错误 我也不明白为什么 重新启动场景的第一个脚本 using UnityEngine using UnityEng
  • 在编译时通过 constexpr 或模板函数获取多维 std::array 的大小

    我用的是三维std array 因为大小在编译时已知 但是 我注意到 size 函数不是静态的 因此 constexpr 模板函数无法访问 我已经找到了下面的演示示例 它估计一维的大小std array 然而 这不适用于二维或更多维度 有没
  • C# 中的并发集合

    我正在寻找一种方法来获得并发收集 in C 或者至少是一个支持的集合并发枚举器 现在我得到了InvalidOperationException当我正在迭代的集合发生变化时 我可以深度复制该集合并使用私人副本 但我想知道是否有更好的方法 代码
  • DisplayMemberPath 在 WPF 中不起作用

    我要显示CustomerList CustomerName财产项目ListBox using ItemsSource DisplayMemberPath仅限财产 但它不起作用 我不想使用DataContext或我的问题中的任何其他绑定 请帮
  • 实体框架 6 和集合

    我正在开发我的第一个实体框架应用程序 我正在使用 EF 版本 6 来自 Nuget 和 net 4 0 然而 我在一些对我来说似乎应该非常简单的事情上遇到了一些困难 我在互联网上发现了很多相互矛盾的建议和解决方案 但是在花了几天时间尝试解决
  • Firebase - Firestore - 使用 collection.add() 获取密钥

    我在使用 Firebase 的新 Firestore 时遇到问题 情况 我有一个collection room 我创建房间collection room add room 我正在尝试做的事情 我需要更新一个房间 为此 我使用 collect
  • 示例 urllib3 和 python 中的线程

    我正在尝试在简单线程中使用 urllib3 来获取多个 wiki 页面 该脚本将 为每个线程创建 1 个连接 我不明白为什么 并永远挂起 urllib3 和线程的任何提示 建议或简单示例 import threadpool from url