I'm working on a project that contain a webserver that handle "time" and "traffic light" parameter and save it to a pickle file and another script load the pickle and use it for mqtt client
import pickle
import paho.mqtt.client as mqtt
from datetime import datetime, date, time
from threading import Timer
date=datetime.now()
print date
try:
while True:
fp = open("shared.pkl", 'rb')
shared = pickle.load(fp)
if date < shared["now"] :
time= shared["time"]
light = shared["light"]
date = shared["now"]
fp.close()
time= int(time)
def pub (s):
client.publish("traffic/light1", payload = s ,qos=0, retain=False)
t= Timer(time , pub,[light])
t.start()
print time
print light
print date
client = mqtt.Client()
client.connect("localhost", 1883, 60)
# 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(timeout=1.0, max_packets=1)
except (EOFError, UnpicklingError):
pass
it works good but sometimes it doesn't publish or doesn't read the pkl file!! any suggestions?