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 上找到