mrq
v0.9.10
MRQ 是一个基于 mongo、redis 和 gevent 构建的 Python 分布式任务队列。
完整文档可在 readthedocs 上找到
MRQ是一个固执己见的任务队列。它的目标是像RQ一样简单漂亮,同时具有接近Celery的性能
MRQ 最初是在 Pricing Assistant 中开发的,其初始功能集满足具有异构作业(IO 密集型和 CPU 密集型、大量小任务和一些大型任务)的工作队列的需求。
这个 5 分钟的教程将向您展示如何使用 MRQ 运行您的第一个作业。
pip install mrq
安装 MRQmongod &
启动 mongo 服务器redis-server &
启动 redis 服务器创建一个新目录并在名为tasks.py
的文件中编写一个简单的任务:
$ mkdir test-mrq && cd test-mrq
$ touch __init__.py
$ vim tasks.py
from mrq . task import Task
import urllib2
class Fetch ( Task ):
def run ( self , params ):
with urllib2 . urlopen ( params [ "url" ]) as f :
t = f . read ()
return len ( t )
您现在可以使用mrq-run
从命令行运行它:
$ mrq-run tasks.Fetch url http ://www.google.com
2014-12-18 15 :44:37.869029 [DEBUG] mongodb_jobs: Connecting to MongoDB at 127.0.0.1:27017/mrq...
2014-12-18 15 :44:37.880115 [DEBUG] mongodb_jobs: ... connected.
2014-12-18 15 :44:37.880305 [DEBUG] Starting tasks.Fetch({'url': 'http://www.google.com'})
2014-12-18 15 :44:38.158572 [DEBUG] Job None success: 0.278229s total
17655
让我们用不同的参数安排相同的任务 3 次:
$ mrq-run --queue fetches tasks.Fetch url http ://www.google.com &&
mrq-run --queue fetches tasks.Fetch url http ://www.yahoo.com &&
mrq-run --queue fetches tasks.Fetch url http ://www.wordpress.com
2014-12-18 15 :49:05.688627 [DEBUG] mongodb_jobs: Connecting to MongoDB at 127.0.0.1:27017/mrq...
2014-12-18 15 :49:05.705400 [DEBUG] mongodb_jobs: ... connected.
2014-12-18 15 :49:05.729364 [INFO] redis: Connecting to Redis at 127.0.0.1...
5492f771520d1887bfdf4b0f
2014-12-18 15 :49:05.957912 [DEBUG] mongodb_jobs: Connecting to MongoDB at 127.0.0.1:27017/mrq...
2014-12-18 15 :49:05.967419 [DEBUG] mongodb_jobs: ... connected.
2014-12-18 15 :49:05.983925 [INFO] redis: Connecting to Redis at 127.0.0.1...
5492f771520d1887c2d7d2db
2014-12-18 15 :49:06.182351 [DEBUG] mongodb_jobs: Connecting to MongoDB at 127.0.0.1:27017/mrq...
2014-12-18 15 :49:06.193314 [DEBUG] mongodb_jobs: ... connected.
2014-12-18 15 :49:06.209336 [INFO] redis: Connecting to Redis at 127.0.0.1...
5492f772520d1887c5b32881
您可以看到, mrq-run
没有执行任务并立即返回其结果,而是将它们添加到名为fetches
队列中并打印了它们的 ID。
现在使用mrq-dashboard &
并在 localhost:5555 上检查新创建的队列和作业
它们已准备好由工作人员出队。使用mrq-worker
启动一个,并在仪表板上跟踪它,因为它并行执行排队的作业。
$ mrq-worker fetches
2014-12-18 15 :52:57.362209 [INFO] Starting Gevent pool with 10 worker greenlets (+ report, logs, adminhttp)
2014-12-18 15 :52:57.388033 [INFO] redis: Connecting to Redis at 127.0.0.1...
2014-12-18 15 :52:57.389488 [DEBUG] mongodb_jobs: Connecting to MongoDB at 127.0.0.1:27017/mrq...
2014-12-18 15 :52:57.390996 [DEBUG] mongodb_jobs: ... connected.
2014-12-18 15 :52:57.391336 [DEBUG] mongodb_logs: Connecting to MongoDB at 127.0.0.1:27017/mrq...
2014-12-18 15 :52:57.392430 [DEBUG] mongodb_logs: ... connected.
2014-12-18 15 :52:57.523329 [INFO] Fetching 1 jobs from ['fetches']
2014-12-18 15 :52:57.567311 [DEBUG] Starting tasks.Fetch({u'url': u'http://www.google.com'})
2014-12-18 15 :52:58.670492 [DEBUG] Job 5492f771520d1887bfdf4b0f success: 1.135268s total
2014-12-18 15 :52:57.523329 [INFO] Fetching 1 jobs from ['fetches']
2014-12-18 15 :52:57.567747 [DEBUG] Starting tasks.Fetch({u'url': u'http://www.yahoo.com'})
2014-12-18 15 :53:01.897873 [DEBUG] Job 5492f771520d1887c2d7d2db success: 4.361895s total
2014-12-18 15 :52:57.523329 [INFO] Fetching 1 jobs from ['fetches']
2014-12-18 15 :52:57.568080 [DEBUG] Starting tasks.Fetch({u'url': u'http://www.wordpress.com'})
2014-12-18 15 :53:00.685727 [DEBUG] Job 5492f772520d1887c5b32881 success: 3.149119s total
2014-12-18 15 :52:57.523329 [INFO] Fetching 1 jobs from ['fetches']
2014-12-18 15 :52:57.523329 [INFO] Fetching 1 jobs from ['fetches']
一旦工作完成,您可以使用 Ctrl-C 来中断它。
这是 MRQ 基本功能的预览。它真正有用的是:
mrq-run
。这些功能将在未来的简单网络爬虫示例中进行演示。
完整文档可在 readthedocs 上找到