Skip to content

Instantly share code, notes, and snippets.

@houssemFat
Last active November 10, 2016 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save houssemFat/a2c60a775324db929455eb3c59f641e6 to your computer and use it in GitHub Desktop.
Save houssemFat/a2c60a775324db929455eb3c59f641e6 to your computer and use it in GitHub Desktop.
how to run 6.4 mqtt client connection with mqq
import paho.mqtt.client as mqtt
from os import path
import json
BASE_URL = "" ;
CHANNEL_ALL = 'common'
CHANNEL_REPLY = 'reply'
CLIENT = '/me-it'
class Client:
def __init__(self, name, suffix =""):
self.name = 'python_%s_%s' % (suffix, str(name))
print self.name
# The callback for when the client receives a CONNACK response from the server.
def on_connect(self, client, userdata, flags, rc):
print self.name
common_channel = '/'.join ([CLIENT, CHANNEL_ALL]);
client.subscribe(common_channel);
"""
Subscribing in on_connect() means that if we lose the connection and
reconnect then subscriptions will be renewed.
client.subscribe("$SYS/#")
$SYS/ResourceStatistics/Memory
$SYS/ResourceStatistics/Store
$SYS/ResourceStatistics/Endpoint
"""
# The callback for when a PUBLISH message is received from the server.
def on_message(self, client, userdata, msg):
print "on_message"
"""outfolder = path.dirname(path.dirname(path.realpath(__file__)))
outfolder = path.join(outfolder, 'out' , self.name )
# + '_' + msg.topic.replace('/', '--' )
print outfolder
with open (outfolder, 'a') as out :
out.write(str(msg.payload))
"""
reply_channel = '/'.join ([CLIENT, CHANNEL_REPLY]);
message = json.loads (msg.payload);
message ['s'] = "1";
message ["u"] = self.name;
self.client.publish(reply_channel, json.dumps(message));
def connect (self):
print "starting connecting %s" % self.name
client = mqtt.Client(self.name)
client.on_connect = self.on_connect
client.on_message = self.on_message
client.connect(BASE_URL, 16101, 60)
self.client = client
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_start()
from client import Client
import random
from multiprocessing import Process, Pool, freeze_support
def run(arg):
print "Starting >>>"
for i in range (1, 801) :
a = Client (i, str(arg))
a.connect()
#while(True) :
# continue
if __name__ == '__main__':
p = Pool(8)
freeze_support()
"""
def f ():
p = Process(target=run)
p.start()
p.join()
"""
p.map(run, range(1, 9))
while(True) :
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment