scrapy之parallel

Limiting Parallelism
jcalderone
May 22nd, 2006

This blog has moved! Read this post and its comments at its new home. Concurrency can be a great way to speed things up, but what happens when you have too much concurrency? Overloading a system or a network can be detrimental to performance. Often there is a peak in performance at a particular level of concurrency. Executing a particular number of tasks in parallel will be easier than ever with Twisted 2.5 and Python 2.5:

from twisted.internet import defer, task

def parallel(iterable, count, callable, *args, **named):
    coop = task.Cooperator()
    work = (callable(elem, *args, **named) for elem in iterable)
    return defer.DeferredList([coop.coiterate(work) for i in xrange(count)])

Here‘s an example of using this to save the contents of a bunch of URLs which are listed one per line in a text file, downloading at most fifty at a time:

from twisted.python import log
from twisted.internet import reactor
from twisted.web import client

def download((url, fileName)):
    return client.downloadPage(url, file(fileName, ‘wb‘))

urls = [(url, str(n)) for (n, url) in enumerate(file(‘urls.txt‘))]
finished = parallel(urls, 50, download)
finished.addErrback(log.err)
finished.addCallback(lambda ign: reactor.stop())
reactor.run()

[Edit: The original generator expression in this post was of the form ((yield foo()) for x in y). The yield here is completely superfluous, of course, so I have removed it.]

from twisted.internet import defer, reactor, task
l=[3,4,5,6]
def f(a):
    print a
work = (f(elem) for elem in l)
for i in range(3):
    work.next()

coop = task.Cooperator()
#work = (callable(elem, *args, **named) for elem in iterable)
d=[coop.coiterate(work) for _ in range(5)]
print d

[<Deferred at 0x1aa0c88 waiting on Deferred at 0x1aa0d50>, <Deferred at 0x1aa0dc8 waiting on Deferred at 0x1aa0e90>, <Deferred at 0x1aa0f30 waiting on Deferred at 0x1aa4030>, <Deferred at 0x1aa40d0 waiting on Deferred at 0x1aa4198>, <Deferred at 0x1aa4238 waiting on Deferred at 0x1aa4300>]

原文地址:https://www.cnblogs.com/pyhai/p/8711369.html

时间: 2024-08-30 13:33:35

scrapy之parallel的相关文章

Scrapy

一.安装 Linux pip3 install scrapy Windows a. pip3 install wheel b. 下载twisted http://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted c. 进入下载目录,执行 pip3 install Twisted?17.1.0?cp35?cp35m?win_amd64.whl d. pip3 install scrapy e. 下载并安装pywin32:https://sourceforge.

爬虫必备—Scrapy

一.Scrapy简介 Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的, 也可以应用在获取API所返回的数据(例如 Amazon Associates Web Services ) 或者通用的网络爬虫.Scrapy用途广泛,可以用于数据挖掘.监测和自动化测试. Scrapy 使用了 Twisted异步网络库来处理网络通讯.整体架构大致如下: Scrapy主

爬虫——Scrapy框架案例二:阳光问政平台

阳光热线问政平台 URL地址:http://wz.sun0769.com/index.php/question/questionType?type=4&page= 爬取字段:帖子的编号.投诉类型.帖子的标题.帖子的URL地址.部门.状态.网友.时间. 1.items.py # -*- coding: utf-8 -*- # Define here the models for your scraped items # # See documentation in: # http://doc.sc

【scrapy实践】_爬取安居客_广州_新楼盘数据

需求:爬取[安居客-广州-新楼盘]的数据,具体到每个楼盘的详情页的若干字段. 难点:楼盘类型各式各样:住宅 别墅 商住 商铺 写字楼,不同楼盘字段的名称不一样.然后同一种类型,比如住宅,又分为不同的情况,比如分为期房在售,现房在售,待售,尾盘.其他类型也有类似情况.所以字段不能设置固定住. 解决方案:目前想到的解决方案,第一种:scrapy中items.py中不设置字段,spider中爬的时候自动识别字段(也就是有啥字段就保留下来),然后返回字典存起来.第二种,不同字段的网页分别写规则单独抓取.

Scrapy抓取Quotes to Scrape

# 爬虫主程序quotes.py # -*- coding: utf-8 -*- import scrapy from quotetutorial.items import QuoteItem # 启动爬虫 # 请求都是默认的,我们不需要管请求的操作,只要关心解析的过程就可以了 class QuotesSpider(scrapy.Spider): name = "quotes" allowed_domains = ["quotes.toscrape.com"] st

爬虫——Scrapy框架案例一:手机APP抓包

以爬取斗鱼直播上的信息为例: URL地址:http://capi.douyucdn.cn/api/v1/getVerticalRoom?limit=20&offset=0 爬取字段:房间ID.房间名.图片链接.存储在本地的图片路径.昵称.在线人数.城市 1.items.py # -*- coding: utf-8 -*- # Define here the models for your scraped items # # See documentation in: # http://doc.s

scrapy实战1分布式爬取有缘网:

直接上代码: items.py 1 # -*- coding: utf-8 -*- 2 3 # Define here the models for your scraped items 4 # 5 # See documentation in: 6 # http://doc.scrapy.org/en/latest/topics/items.html 7 8 import scrapy 9 10 11 class YouyuanwangItem(scrapy.Item): 12 # defin

Scrapy基础01

一.Scarpy简介 Scrapy基于事件驱动网络框架 Twisted 编写.(Event-driven networking) 因此,Scrapy基于并发性考虑由非阻塞(即异步)的实现. 参考:武Sir笔记 参考:Scrapy 0.25 文档 参考:Scrapy架构概览 二.爬取chouti.com新闻示例 # chouti.py # -*- coding: utf-8 -*- import scrapy from scrapy.http import Request from scrapy.

python——Scrapy 框架

爬虫的自我修养_4 一.Scrapy 框架简介 Scrapy是用纯Python实现一个为了爬取网站数据.提取结构性数据而编写的应用框架,用途非常广泛. 框架的力量,用户只需要定制开发几个模块就可以轻松的实现一个爬虫,用来抓取网页内容以及各种图片,非常之方便. Scrapy 使用了 Twisted['tw?st?d](其主要对手是Tornado)异步网络框架来处理网络通讯,可以加快我们的下载速度,不用自己去实现异步框架,并且包含了各种中间件接口,可以灵活的完成各种需求. Scrapy架构图(绿线是