MemcacheQ安裝及使用方法
一. 安裝
MemcacheQ 是一個簡單的分布式隊(duì)列服務(wù),它的運(yùn)行依賴于BerkeleyDB 和 libevent,所以需要先安裝BerkeleyDB和libevent.
Berkeley DB 4.7 or later
Download from <http://www.oracle.com/database/berkeley-db/db/index.html>
How to install BerkeleyDB:
$tar -xvzf db-5.3.21.tar.gz $cd db-5.3.21/ $cd build_unix/ $../dist/configure $make $make install
安裝BerkeleyDB時,可以手動指定安裝路徑:
../dist/configure --prefix=/usr/local/berkeleydb
不指定的話,默認(rèn)安裝在:/usr/local/BerkeleyDB.5.3
libevent 1.4.x or later
先檢查libevent 是否已經(jīng)安裝:
#rpm -qa|grep libevent
libevent-devel-2.0.10-2.fc15.x86_64
libevent-2.0.10-2.fc15.x86_64
libevent-2.0.10-2.fc15.i686
或者:
ls -al /usr/lib |grep libevent
如果還沒有安裝:
Download from <http://monkey.org/~provos/libevent/>
How to install libevent:
$tar -xvzf libevent-1.4.x-stable.tar.gz $cd libevent-1.4.x-stable $./configure $make $make install
安裝libevent時,可以手動指定安裝路徑:
./configure --prefix=/usr/local/libevent
不指定的話,默認(rèn)安裝在:/usr/lib64(64位系統(tǒng))或者/usr/lib(32位系統(tǒng))
memcacheQ
下載軟件包:http://code.google.com/p/memcacheq/downloads/list
解壓縮,cd進(jìn)目錄
./configure --enable-threads make make install
configure 時,如果libevent 不是安裝在默認(rèn)目錄,需--with--libevent=/usr/local/libevent指定libevent的安裝目錄
若沒有將
/usr/local/lib
/usr/local/BerkeleyDB.5.3/lib
添加進(jìn)/etc/ld.so.conf 并運(yùn)行 /sbin/ldconfig 則需--with-bdb=/usr/local/BerkeleyDB.5.3 指定berkeleyDb庫的路徑
二.使用
啟動memcacheQ
使用memcacheq -h 的命令來查看命令行選項(xiàng)
啟動memcacheq:memcacheq -d -u nobody -r -H /tmp/memcacheq -N -R -v -L 1024 -B 1024 > /tmp/mq_error.log 2>&1
啟動時需-u 參數(shù),指定運(yùn)行memcacheQ的用戶,且指定的用戶必須有數(shù)據(jù)文件的讀寫權(quán)限,如這里的/tmp/memcacheQ目錄,否則不能啟動
命令行使用memcacheQ
telnet 127.0.0.1 22202
Trying 127.0.0.1…
Connected to 127.0.0.1.
Escape character is ‘^]'.
只有兩個命令可以在命令行下使用memcacheQ
寫對列:
set <queue name> <flags> 0 <message_len>\r\n
<put your message body here>\r\n
STORED\r\n
取出隊(duì)列:
get <queue name>\r\n
VALUE <queue name> <flags> <message_len>\r\n
<your message body will come here>\r\n
END\r\n
與memcache協(xié)議基本一致,只是把key name換成queue name,而且在set的命令中,忽略了expire_time的參數(shù)。mq的數(shù)據(jù)存儲是存在berkeleyDB中,做了持久化存儲,沒有內(nèi)存的過期時間。
示例:
telnet 127.0.0.1 22202
Trying 127.0.0.1…
Connected to 127.0.0.1.
Escape character is ‘^]'.
set q4 0 0 5
hello
STORED
set q4 0 0 5
world
STORED
stats queue
STAT q4 2/0
END
get q4
VALUE q4 0 5
hello
END
stats queue
STAT q4 2/1
END
三.安裝使用過程中可能出現(xiàn)的錯誤
1.編譯出現(xiàn)錯誤:checking for library containing db_create... no
configure: error: cannot find libdb.so in /usr/local/BerkeleyDB.5.3/lib
需要修改 configure 中的BerkeleyDB中的預(yù)編譯參數(shù)vim configure找到bdbdir="/usr/local/berkeleydb"改為
bdbdir="/usr/local/BerkeleyDB.5.3"再次編譯
2.configure: error: cannot find libdb.so in /usr/local/BerkeleyDB.5.3/lib
出現(xiàn)此錯誤的原因在于沒有安裝BerkyleyDb,安裝即可
3./usr/local/memcacheq/bin/memcachq -h
運(yùn)行報(bào):
memcacheq: error while loading shared libraries: libdb-5.3.so: cannot open shared object file: No such file or directory
解決方法:ln -s /usr/local/BerkeleyDB.5.3/lib/libdb-5.3.so /usr/lib/libdb-5.3.so
注:在64位操作系統(tǒng)中,需執(zhí)行
ln -s /usr/local/BerkeleyDB.5.3/lib/libdb-5.3.so /usr/lib64/libdb-5.3.so
使用memcacheq做異步隊(duì)列,做個簡單的生產(chǎn)者-消費(fèi)者模型。生產(chǎn)者將數(shù)據(jù)寫入mq中,消費(fèi)者異步去隊(duì)列中去取數(shù)據(jù),進(jìn)而進(jìn)一步的消費(fèi)處理數(shù)據(jù)。
#!/usr/bin/env python #-*- coding:utf8 -*- import sys import time import random import memcache mc = memcache.Client(["%s:%s"%("127.0.0.1", "22202")]) queue_name = "q1" def putter(): count = 0 while True: data = "hello%d"%(count) mc.set(queue_name, data) print "put ", data count += 1 time.sleep(random.randint(1, 10)) def process_data(data): print "processing data :", data def getter(): while True: data = mc.get(queue_name) if data: process_data(data) else: print "no message, sleep for a while ..." time.sleep(30) if __name__ == "__main__": if len(sys.argv) != 2: print "Wrong arg numbers" else: cmd = sys.argv[1] if cmd == "put": putter() elif cmd == "get": getter() else: print "wrong cmd"
在使用時,開兩個終端模擬兩個進(jìn)程,在一個終端中運(yùn)行
python mqdemo.py put
來模擬生產(chǎn)者;另一個終端中運(yùn)行
python mqdemo.py get
模擬消費(fèi)者。
相關(guān)文章
Linux 創(chuàng)建子進(jìn)程執(zhí)行任務(wù)的實(shí)現(xiàn)方法
下面小編就為大家分享一篇Linux 創(chuàng)建子進(jìn)程執(zhí)行任務(wù)的實(shí)現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助,一起跟隨小編過來看看吧2018-01-01Linux安裝NodeJs并配合Nginx實(shí)現(xiàn)反向代理
本篇文章主要介紹了Linux安裝NodeJs并配合Nginx實(shí)現(xiàn)反向代理,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2016-11-11Xshell7遠(yuǎn)程連接失敗(connection failed)的問題解決
本文主要介紹了Xshell7遠(yuǎn)程連接失敗(connection failed)的問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Linux網(wǎng)橋配置br-lan、eth0、eth1、ra0、rai0方式
這篇文章主要介紹了Linux網(wǎng)橋配置br-lan、eth0、eth1、ra0、rai0方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05解決Ubuntu 18.04安裝VMwareTools錯誤問題
這篇文章主要介紹了Ubuntu 18.04安裝VMwareTools錯誤的解決方法,需要的朋友可以參考下2019-11-11