php 代码
php_queue_test.php
1 <?php
2 $me = new Memcache;
3 $me->connect("127.0.0.1", "22202") or die;
4 $count = 1;
5 while(true) {
6 $queue_name = "q2";
7 $hello = 'hello,I\'m beck'.$count;
8 memcache_set($me, 'q2', $hello, 0, 0);
9 $count ++;
10 sleep(1);
11 }
12 ?>
php php_queue_test.php
python 代码
py_queue_test.py 1 #!/usr/bin/env python
2 #-*- coding:utf8 -*-
3
4 import sys
5 import time
6 import random
7
8 import memcache
9
10 mc = memcache.Client(["%s:%s"%("127.0.0.1", "22202")])
11 queue_name = "q2"
12 def putter():
13 count = 0
14 while True:
15 data = "{url:aad, type:curl}hello%d"%(count)
16 mc.set(queue_name, data)
17 print "put ", data
18 count += 1
19 time.sleep(random.randint(1, 10))
20
21 def process_data(data):
22 print "processing data :", data
23
24 def getter():
25 while True:
26 data = mc.get(queue_name)
27 if data:
28 process_data(data)
29 else:
30 print "no message, sleep for a while ..."
31 time.sleep(10)
32
33 if __name__ == "__main__":
34 if len(sys.argv) != 2:
35 print "Wrong arg numbers"
36 else:
37 cmd = sys.argv[1]
38 if cmd == "put": putter()
39 elif cmd == "get": getter()
40 else: print "wrong cmd"ptyhon py_queue_test.py get