# -*- coding:utf-8 -*- from multiprocessing import Process import paho.mqtt.publish as publish import paho.mqtt.subscribe as subscribe import time,json HOST = "127.0.0.1" PORT = 11883 NAME = "mqname" PASSWORD = "mqpwd" TOPIC = "test/hito" client_id = time.strftime('mq:test:%Y%m%d%H%M%S',time.localtime(time.time())) def mqtt_publish(): print "===============>pub" msg = {"msg_id": "101343506507657"} msg = json.dumps(msg) publish.single(TOPIC, msg, qos = 1,hostname=HOST,port=PORT, client_id=client_id,auth = {'username':NAME, 'password':PASSWORD}) print "===============> pub over" def mqtt_subscribe(): print "===============>sub" msg = subscribe.simple(TOPIC, qos = 1,hostname=HOST,port=PORT, client_id=client_id,auth = {'username':NAME, 'password':PASSWORD}) print msg.topic+" :"+msg.payload.decode("utf-8") if __name__ == '__main__': sub=Process(target=mqtt_subscribe) pub=Process(target=mqtt_publish) sub.start() time.sleep(1) pub.start() sub.join() print("main-------")