MemcacheQ安装及使用方法

2019-10-12 14:35:21王旭

示例:
 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

三.安装使用过程中可能出现的错误

1.编译出现错误:checking for library containing db_create... no
configure: error: cannot find libdb.so in /usr/local/BerkeleyDB.5.3/lib
需要修改 configure 中的BerkeleyDB中的预编译参数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
出现此错误的原因在于没有安装BerkyleyDb,安装即可

3./usr/local/memcacheq/bin/memcachq -h
            运行报:
            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位操作系统中,需执行
ln -s /usr/local/BerkeleyDB.5.3/lib/libdb-5.3.so /usr/lib64/libdb-5.3.so

使用memcacheq做异步队列,做个简单的生产者-消费者模型。生产者将数据写入mq中,消费者异步去队列中去取数据,进而进一步的消费处理数据。

#!/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"

在使用时,开两个终端模拟两个进程,在一个终端中运行

python mqdemo.py put

来模拟生产者;另一个终端中运行

python mqdemo.py get

模拟消费者。