python–memcached—activemq

來源:互聯網
上載者:User

1.安裝memcached的python用戶端

 wget ftp://ftp.tummy.com/pub/python-memcached/python-memcached-latest.tar.gz
  python2.x setup.py install

2.安裝activemq的python用戶端

  wget http://stomppy.googlecode.com/files/stomp.py-3.0.3.tar.gz
  python2.x setup.py install --prefix=/usr/local/similarlib/

  stomp官網支援:http://code.google.com/p/stomppy/

3.python-activemq的demo程式

mq_send:

#!/usr/bin/python## Usage: stomp_send.py <msize> <nmsgs>#import sysimport osimport loggingimport stompimport timeimport jsonlogging.basicConfig()nmsgs = int(sys.argv[1])msg = {'pid':1,'id':1,'start_at':"time",'type':'parse_title','articleid':0, 'from':1, 'to':361}dest = '/queue/worker'start = time.time()conn = stomp.Connection([('172.16.1.217', 61612)])conn.start()conn.connect(wait=True)for i in range(nmsgs):msg['articleid'] = iconn.send(json.write(msg), destination=dest)print "send one"conn.disconnect()print "OK Finished msgs %d time %f" % (nmsgs, (time.time()-start))

mq_receive:

import sysimport osimport loggingimport stompimport jsonimport timeclass MyListener(object):def on_error(self, headers, message):print 'received an error %s' % messagedef on_message(self, headers, message):print 'received a message %s' % message#print headers['message-id']#sys.exit(0)conn.ack({'message-id':headers['message-id']})dest = '/queue/test1'logging.basicConfig()conn = stomp.Connection([('172.16.1.217', 61612)])conn.set_listener('', MyListener())conn.start()conn.connect(wait=True)conn.subscribe(destination=dest, ack='client')while True:try:time.sleep(1)except:break

4.python-memcached的demo程式

import time,memcache,pickle,cPickle,sys #,msgpackf = file("/tmp/ids", 'r')mc = memcache.Client(["172.16.1.217:11211"])#count = 0#for i in range(1,10000):#if mc.get("content-%s"%str(i)) != None:#count += 1#f.write("%s\n"%str(i))#if count >= 1000:#break#sys.exit(0)id_list = [int(x.strip()) for x in f]print id_liststart = time.time()results = mc.get_multi(id_list, key_prefix="content-")#for k,v in results.iteritems():#results[k] = cPickle.loads(v)#print v #time.sleep(1)#mc.set_multi(results,key_prefix="content-")print "eats %s s"%str(time.time()-start)print len(results)

下面是一個同時使用mq和memcached的demo:

#!/usr/bin/python##import sysimport osimport loggingimport stompimport timeimport json,memcachemc = memcache.Client(["172.16.1.217:11211"])t = time.time()tstart = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(t))tstart += ".%03d" % ((t-int(t))*1000)start = time.time()maxaid = 10000conn = stomp.Connection([('172.16.1.217', 61612)])dest = '/queue/test_worker_2'conn.start()conn.connect(wait=True)for item in ['title','content','tag','cate','baike']:for i in range(1,25666):if mc.get(item+"-"+str(i)) == None:task = {'id':str(time.time()),'pid':'','start_at':tstart,'type':"parse_"+item,'from':i,'to':i}conn.send(json.write(task), destination=dest)conn.disconnect()

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.