关于详细队列的介绍,也可以参考这篇文章:https://www.cnblogs.com/kerwinC/p/5967584.html
启用消息队列,即便服务器宕机,没有处理的数据仍会得到保留。
使用的是rabbitmq ,以及python内置的pika
利用消息队列去实现推送和获取内容,作为生产者,我们要将所要传输的数据,进行组装,然后放到我们约定好的队列当中,等待消费者去获取。
move_data = []
if platform.platform().find('Windows') == -1: #开发环境运用的是windows,在线下日常开发过程中没必要打开此队列
line = dict()
line['erp_sell_order_num'] = sd.order_id.name
line['erp_wh_code'] = sd.order_id.shipping_rule_id.warehouse_supplier.code
line['erp_company_code'] = sd.company_id.id
line['type'] = 'return'
item_list = []
for icon in sd.line_out_ids:
item = dict()
item['sku'] = icon.goods_id.name
item['qty'] = icon.goods_qty
item['unit_cost_price'] = icon.cost_unit
item['cost_price'] = icon.goods_qty * icon.cost_unit
item['erp_sell_order_id'] = sd.order_id.id
item_list.append(item)
line['ErpSellOrderItems'] = item_list
move_data.append(line)
if move_data:
credentials = pika.PlainCredentials('user', 'password')
connection = pika.BlockingConnection(
pika.ConnectionParameters('xx.xx.xx.xx', 5672, '/', credentials))
channel = connection.channel()
channel.queue_declare(queue='ErpSellerOrder')
channel.basic_publish(exchange='',
routing_key='ErpSellerOrder',
body=json.dumps(move_data))
print u"推送消息队列"
connection.close()
作为消费者,我们从队列之中获取数据,那么我们在处理完数据之后,需要回传给消息队列一个消息,说明这次数据我收到了,那么消息队列便会把这一次的数据给清除掉,不然的话,这次的数据将一直向你推送。
def callback(ch, method, properties, body):
if body:
db = 'demo'
username = 'admin'
password = 'admin'
odoo = odoorpc.ODOO('127.0.0.1', port=5432)
odoo.login(db, username, password)
data = json.loads(body)
datas = dict()
datas['U82ErpList'] = data
# datas = urllib.urlencode(datas)
reponse = odoo.json(url='warehouse/inventory/in', params = datas)
if 'result' in reponse:
demo = json.loads(reponse['result'])
if demo['Status'] == u'Success':
print u'队列结束'
ch.basic_ack(delivery_tag=method.delivery_tag)
else:
ch.basic_ack(delivery_tag=method.delivery_tag)
def the_demo_list():
credentials = pika.PlainCredentials('user', 'password')
connection = pika.BlockingConnection(pika.ConnectionParameters('xx.xx.xx.xx', 5672, '/', credentials))
channel = connection.channel()
channel.queue_declare(queue='U8RdRecord')
channel.basic_consume(callback, queue='U8RdRecord',no_ack=False) #no_ack=False是需要回传消息的,True的话,消息队列发完之后便会删除。
print u'开始监听消息队列'
channel.start_consuming()
if platform.platform().find('Windows')==-1:
print u'启动消息队列'
t = threading.Thread(target=the_demo_list)
t.start()
网友评论