celery的使用
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的使用的更多相关文章
- 异步任务队列Celery在Django中的使用
前段时间在Django Web平台开发中,碰到一些请求执行的任务时间较长(几分钟),为了加快用户的响应时间,因此决定采用异步任务的方式在后台执行这些任务.在同事的指引下接触了Celery这个异步任务队 ...
- celery使用的一些小坑和技巧(非从无到有的过程)
纯粹是记录一下自己在刚开始使用的时候遇到的一些坑,以及自己是怎样通过配合redis来解决问题的.文章分为三个部分,一是怎样跑起来,并且怎样监控相关的队列和任务:二是遇到的几个坑:三是给一些自己配合re ...
- tornado+sqlalchemy+celery,数据库连接消耗在哪里
随着公司业务的发展,网站的日活数也逐渐增多,以前只需要考虑将所需要的功能实现就行了,当日活越来越大的时候,就需要考虑对服务器的资源使用消耗情况有一个清楚的认知. 最近老是发现数据库的连接数如果 ...
- celery 框架
转自:http://www.cnblogs.com/forward-wang/p/5970806.html 生产者消费者模式 在实际的软件开发过程中,经常会碰到如下场景:某个模块负责产生数据,这些数据 ...
- celery使用方法
1.celery4.0以上不支持windows,用pip安装celery 2.启动redis-server.exe服务 3.编辑运行celery_blog2.py !/usr/bin/python c ...
- Celery的实践指南
http://www.cnblogs.com/ToDoToTry/p/5453149.html Celery的实践指南 Celery的实践指南 celery原理: celery实际上是实现了一个典 ...
- Using Celery with Djang
This document describes the current stable version of Celery (4.0). For development docs, go here. F ...
- centos6u3 安装 celery 总结
耗时大概6小时. 执行 pip install celery 之后, 在 mac 上 celery 可以正常运行, 在 centos 6u3 上报错如下: Traceback (most recent ...
- celery 异步任务小记
这里有一篇写的不错的:http://www.jianshu.com/p/1840035cb510 自己的"格式化"后的内容备忘下: 我们总在说c10k的问题, 也做了不少优化, 然 ...
- Celery 框架学习笔记
在学习Celery之前,我先简单的去了解了一下什么是生产者消费者模式. 生产者消费者模式 在实际的软件开发过程中,经常会碰到如下场景:某个模块负责产生数据,这些数据由另一个模块来负责处理(此处的模块是 ...
随机推荐
- Python冒泡算法和修改配置文件
先学习之前未完成的冒泡算法 li = [13,22,6,99,11] 从小到大 从第一个数字比较把大的往后移位 for m in range(4): num1 = li[m] num2 = li[m+ ...
- 【持久化框架】Mybatis简介与原理
从这篇博文开始我们学习一下Mybatis,希望大家提出宝贵的建议. 什么是Mybatis MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache softwar ...
- 学生成绩管理C++版
[标题]学生成绩管理的设计与实现 [开发语言]C++ [主要技术]STL [概要设计]类名:student 类成员:No.Name.Math.Eng.Chn.Cpro.Sum 成员函数:getname ...
- 配置网络yum源
有的时候服务器需要下载一些软件,是本地yum源没有的,这时候需要配置网络yum源 #cd /etc/yum.repos.d/ 把这个目录下的配置文件备份,改名:或者是把文件里边的enabled=1的 ...
- 关于struts2 Could not find action or result错误
今天来配置这个S2SH框架的的时候,刚把环境搭建好,启动时并没有报错,但是当我写了一个action,我也准备通过这个action来访问页面,但是这里我访问的时候却给我报Could not find a ...
- 使用gzip优化web应用(filter实现)
相关知识: gzip是http协议中使用的一种加密算法,客户端向web服务器端发出了请求后,通常情况下服务器端会将页面文件和其他资源,返回到客户端,客户端加载后渲染呈现,这种情况文件一般都比较大,如果 ...
- React+ajax+java 上传图片并预览
之前有在网上找ajax上传图片的资料,大部分的人写得都是用jQuery,但是在这里用JQuery就大才小用了,所以我就自己写了,先上图. 由上图,首先点击上面的选择文件,在选择图片之后,将会自动上传图 ...
- JMeter-Eclipse添加自定义函数 MD5加密 32位和16位
最近公司的接口都是MD5 16位加密,所以要使用加密功能. 之前也做过加密,因为用的比较少,所以是写了一个加密方法,导出JAR包,调用的.用起来需要很多设置,并且换算效率也不高.听前同事说,jmet ...
- springboot 集成spring-session redis 实现分布式session
gradle 添加依赖 compile("org.springframework.session:spring-session:1.3.0.RELEASE") compile(&q ...
- grok 匹配log4j
input { file { codec => multiline { pattern => "^\[2016" negate => true what => ...