class RedisUtils(): def __init__(self, host): self.conn = RedisSMQ(host=host, qname="accounts") def redis_enqueue(self, msg): self.conn.sendMessage(delay=0).message(msg).execute() def redis_dequeue(self): return self.conn.popMessage().execute()['message'] def redis_queue_length(self): return self.conn.getQueueAttributes().execute()['msgs'] def redis_quit(self): return self.conn.quit() def redis_clear_queue(self): self.conn.deleteQueue().exceptions(False).execute() self.conn.createQueue(delay=0).vt(20).execute()
class MsgSender: def __init__(self, host, port='6379', qname='message_sender'): self.queue = RedisSMQ(host=host, port=port, qname=qname) self.msg = [] try: # 删除queue如果已经存在 self.queue.deleteQueue().exceptions(False).execute() except Exception as e: print(e) try: # 创建queue self.queue.createQueue(delay=0).vt(0).maxsize(-1).execute() except Exception as e: print(e) def send_result(self, result): message_id = self.queue.sendMessage(delay=0).message( str(result)).execute() self.msg.append(message_id) if len(self.msg) > 20: rt = self.queue.deleteMessage(id=self.msg[0]).execute() if rt: print("RedisSMQ send_result block") del self.msg[0]
import multiprocessing from rsmq import RedisSMQ import base64 smpl_model_used = load_model( '/home/feng/human_zj/SMPL_python_v.1.0.0/smpl/models/basicmodel_m_lbs_10_207_0_v1.0.0.pkl' ) outmesh_path = '/media/ramdisk/result.obj' QUEUE1 = "smplOrigQue" QUEUE2 = "smplObjQue" queue1 = RedisSMQ(host="127.0.0.1", qname=QUEUE1) queue2 = RedisSMQ(host="127.0.0.1", qname=QUEUE2) try: queue1.deleteQueue().exceptions(False).execute() except Exception as e: print(e) try: queue1.createQueue(delay=0).vt(0).maxsize(-1).execute() except Exception as e: print(e) try: queue2.deleteQueue().exceptions(False).execute() except Exception as e: print(e) try: queue2.createQueue(delay=0).vt(0).maxsize(-1).execute()
class RSMQueue(object): _msg = [] def __init__(self, qname, host=DEFAULT['server']): # host 改为对应的ip self.host = host self.qname = qname self.queue = RedisSMQ(host=host, qname=qname) self.consumer = None self.callback = None try: self.queue.deleteQueue().execute() except Exception as e: logging.error('[Exception] RSMQueue deleteQueue: %s', e) print('[Exception] RSMQueue deleteQueue: %s', e) try: self.queue.createQueue(delay=0).maxsize(-1).vt(0).execute() except Exception as e: logging.error('[Exception] RSMQueue createQueue: %s', e) print('[Exception] RSMQueue createQueue: %s', e) def set_callback(self, callback): self.callback = callback def publish(self, message): message_id = self.queue.sendMessage(delay=0).message(message).execute() self._msg.append(message_id) while len(self._msg) > 1: print(self._msg) try: self.queue.deleteMessage(id=self._msg[0]).execute() del self._msg[0] except Exception as e: logging.error('[Exception] RSMQueue publish: %s', e) print('[Exception] RSMQueue publish: %s', e) return message_id def deleteMessage(self, mid): return self.queue.deleteMessage(id=mid).execute() def subscribe1(self, qname, callback): self.consumer = RedisSMQConsumerThread(qname, callback, host=DEFAULT['server']) self.consumer.start() return self.consumer def receiveMessage(self, callback): try: id, message, rc, ts = self.queue.popMessage().execute() if callback and callable(callback): callback(message) except Exception as e: print('[Exception] receivemessage', e) def subscribe(self, callback, freq=10): queue = self.queue def f(callback): while True: try: rt = queue.popMessage().execute() print(rt) if rt['id'] and callback and callable(callback): callback(rt['message']) except Exception as e: print('[Exception] receivemessage', e) pass time.sleep(1 / freq) t = Thread(target=f, args=(callback, )) t.start() return t def cancel_subscribe(self): if self.consumer: self.consumer.stop() def peak(self): def _peak(id, message, rc, ts): print("\t\tpeak", id, message, rc, ts) time.sleep(0.1) return False self.subscribe(_peak)
def main(argv=None): if argv is None: argv = sys.argv ''' Parse args and run producer ''' parser = argparse.ArgumentParser() parser.add_argument("-q", "--queue", dest="queue", action="store", default="queue", help="queue name [default: %(default)s]") parser.add_argument("-n", "--namespace", dest="ns", action="store", default="test", help="queue namespace [default: %(default)s]") parser.add_argument( "-c", "--count", dest="count", action="store", type=int, default=0, help="number of messages to send. If less than 1, send continuously " + "[default: %(default)s]") parser.add_argument( "-i", "--interval", dest="interval", type=float, default=1.5, help="Interval, in seconds, to send [default: %(default)s]") parser.add_argument( "-d", "--delay", dest="delay", type=int, default=0, help="delay, in seconds, to send message with [default: %(default)s]") parser.add_argument("-v", "--visibility_timeout", dest="vt", type=int, default=None, help="Visibility Timeout[default: %(default)s]") parser.add_argument("--delete", dest="delete", action="store_true", default=False, help="If set, delete queue first") parser.add_argument("--no-trace", dest="trace", action="store_false", default=True, help="If set, hide trace messages") parser.add_argument("-H", dest="host", default="127.0.0.1", help="Redis Host [default: %(default)s]") parser.add_argument("-P", dest="port", type=int, default=6379, help="Redis Port [default: %(default)s]") # Parse command line args` args = parser.parse_args() # Create RedisSMQ queue controller LOG.info( "Creating RedisSMQ controller for redis at %s:%s, using default queue: %s:%s", args.host, args.port, args.ns, args.queue) rsqm = RedisSMQ(qname=args.queue, host=args.host, port=args.port, ns=args.ns, vt=args.vt, delay=args.delay, trace=args.trace) if args.delete: rsqm.deleteQueue(qname=args.queue, quiet=True).exceptions(False).execute() # Create queue if it is missing. Swallow errors if already exists rsqm.createQueue(qname=args.queue, quiet=True).exceptions(False).execute() # Start Producing produce(rsqm, "%s:%s" % (args.ns, args.queue), args.count, args.interval)