1.celery的任务调度

# -*- coding: utf-8 -*-
import threading from bs4 import BeautifulSoup
from tornado import httpclient
from celery import Celery
from tornado.httpclient import HTTPClient broker = 'redis://localhost:6379'
backend = 'redis://localhost:6379' app = Celery('tasks', broker=broker, backend=backend) visited = {} @app.task
def get_html(url):
http_client = HTTPClient()
try:
response = http_client.fetch(url, follow_redirects=True)
return response.body
except httpclient.HTTPError as e:
return None
finally:
http_client.close() def start(url):
threads = []
for i in range(20):
t = threading.Thread(target=schedule, args=(url,))
t.daemon = True
t.start()
threads.append(t) for thread in threads:
thread.join() def process_html(url, html):
print url + ": " + html
_add_links_to_queue(url, html) def schedule(url):
print "before call _work " + url
_worker.delay(url)
print "after call _work " + url def _add_links_to_queue(url, html):
soup = BeautifulSoup(html)
links = soup.find_all('a')
for link in links:
try:
_url = link['href']
except:
pass if not _url.startswith('http'):
_url = 'http://' + _url
print url + "==>" + _url
schedule(_url) @app.task
def _worker(url):
print str(threading.currentThread()) + " running " + url
while 1:
if url in visited:
continue
result = get_html.delay(url)
try:
html = result.get(timeout=5)
except Exception as e:
print(url)
print(e)
finally:
process_html(url, html)
visited[url] = True if __name__ == '__main__':
start("http://www.hao123.com/")

  

2.celery如何进行负载均衡设计

celery有send_task方式去做任务调度,因此,负载均衡的话,可以采用自己的算法去做任务分配,可参考:http://blog.csdn.net/vintage_1/article/details/47664187

celery的使用的更多相关文章

  1. 异步任务队列Celery在Django中的使用

    前段时间在Django Web平台开发中,碰到一些请求执行的任务时间较长(几分钟),为了加快用户的响应时间,因此决定采用异步任务的方式在后台执行这些任务.在同事的指引下接触了Celery这个异步任务队 ...

  2. celery使用的一些小坑和技巧(非从无到有的过程)

    纯粹是记录一下自己在刚开始使用的时候遇到的一些坑,以及自己是怎样通过配合redis来解决问题的.文章分为三个部分,一是怎样跑起来,并且怎样监控相关的队列和任务:二是遇到的几个坑:三是给一些自己配合re ...

  3. tornado+sqlalchemy+celery,数据库连接消耗在哪里

    随着公司业务的发展,网站的日活数也逐渐增多,以前只需要考虑将所需要的功能实现就行了,当日活越来越大的时候,就需要考虑对服务器的资源使用消耗情况有一个清楚的认知.     最近老是发现数据库的连接数如果 ...

  4. celery 框架

    转自:http://www.cnblogs.com/forward-wang/p/5970806.html 生产者消费者模式 在实际的软件开发过程中,经常会碰到如下场景:某个模块负责产生数据,这些数据 ...

  5. celery使用方法

    1.celery4.0以上不支持windows,用pip安装celery 2.启动redis-server.exe服务 3.编辑运行celery_blog2.py !/usr/bin/python c ...

  6. Celery的实践指南

    http://www.cnblogs.com/ToDoToTry/p/5453149.html Celery的实践指南   Celery的实践指南 celery原理: celery实际上是实现了一个典 ...

  7. Using Celery with Djang

    This document describes the current stable version of Celery (4.0). For development docs, go here. F ...

  8. centos6u3 安装 celery 总结

    耗时大概6小时. 执行 pip install celery 之后, 在 mac 上 celery 可以正常运行, 在 centos 6u3 上报错如下: Traceback (most recent ...

  9. celery 异步任务小记

    这里有一篇写的不错的:http://www.jianshu.com/p/1840035cb510 自己的"格式化"后的内容备忘下: 我们总在说c10k的问题, 也做了不少优化, 然 ...

  10. Celery 框架学习笔记

    在学习Celery之前,我先简单的去了解了一下什么是生产者消费者模式. 生产者消费者模式 在实际的软件开发过程中,经常会碰到如下场景:某个模块负责产生数据,这些数据由另一个模块来负责处理(此处的模块是 ...

随机推荐

  1. Java IO详解(一)------字节输入输出流

    File 类的介绍:http://www.cnblogs.com/ysocean/p/6851878.html Java IO 流的分类介绍:http://www.cnblogs.com/ysocea ...

  2. Customer segmentation – LifeCycle Grids with R(转)

    I want to share a very powerful approach for customer segmentation in this post. It is based on cust ...

  3. 【T-SQL】系列文章全文目录(2017-06-02更新)

    本系列[T-SQL]主要是针对T-SQL的总结. T-SQL基础 [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础]02.联接查询 [T-SQL基础]03.子查询 [T-SQL基础 ...

  4. 互联网“剁手”新方向,VR全景购物忙——全景智慧城市常诚

    随着VR和AR技术的兴起,各行各业都在寻求VR+的对接方式,除了游戏和社交平台,另一大对VR有着浓厚兴趣的就是电商平台了,阿里.京东等电商巨头纷纷成立VR事业部,如何让亿万用户在VR中愉快的买买买,已 ...

  5. 史上最完整Hadoop2.x完全分布式安装部署-小白也能学会

    一.环境要求: 1.        虚拟机安装并设置网络: 2.        修改主机地址映射: 3.        必备软件:Jdk.Development Tools   Development ...

  6. Selenium基础知识

    本人博客文章网址:https://www.peretang.com/basic-knowledge-of-selenium/ 什么是Selenium Selenium是一个自动化测试工具 是一组不同的 ...

  7. 网页中使用CSS和JS阻止用户选择内容

    CSS实现 body{ -webkit-user-select:none; -moz-user-select:none; -ms-user-select:none; user-select:none; ...

  8. Java中4种类型的内部类 .

    在Java中有4种不同类型的内部类可以使用.下面给出它们的名称和例子. 1.静态嵌套类(Static Nested Classes) class Outer { static class Inner  ...

  9. Coursera 机器学习笔记(四)

    主要为第六周内容机器学习应用建议以及系统设计. 下一步做什么 当训练好一个模型,预测未知数据,发现结果不如人意,该如何提高呢? 1.获得更多的训练实例 2.尝试减少特征的数量 3.尝试获得更多的特征 ...

  10. [IR] BWT+MTF+AC

    BWT (Burrows–Wheeler_transform)数据转换算法 MTF(Move-to-front transform)数据转换 基于统计的压缩算法:游程编码 良心PPT: bwt_bas ...