读取 pickle 文件响应缓慢或无响应
Slow or no response from read pickle file
我正在开发一个包含处理 "time" 和 "traffic light" 参数并将其保存到 pickle 文件和另一个脚本加载 pickle 并将其用于 mqtt 客户端的网络服务器的项目
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
它运行良好,但有时不发布或不读取 pkl 文件!!
有什么建议吗?
不用仔细看,像下面这样的东西应该可以完成工作。
调用 client.loop()
只会处理一些网络流量,不能保证调用后所有流量都已发送。根据您是否需要 asynchronous/blocking 行为,使用 loop_start()
或 loop_forever()
。
client = mqtt.Client()
client.connect("localhost", 1883, 60)
client.loop_start()
print date
while True:
try:
fp = open("shared.pkl", 'rb')
shared = pickle.load(fp)
fp.close()
except (EOFError, UnpicklingError):
continue
if date < shared["now"] :
time= shared["time"]
light = shared["light"]
date = shared["now"]
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
我正在开发一个包含处理 "time" 和 "traffic light" 参数并将其保存到 pickle 文件和另一个脚本加载 pickle 并将其用于 mqtt 客户端的网络服务器的项目
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
它运行良好,但有时不发布或不读取 pkl 文件!! 有什么建议吗?
不用仔细看,像下面这样的东西应该可以完成工作。
调用 client.loop()
只会处理一些网络流量,不能保证调用后所有流量都已发送。根据您是否需要 asynchronous/blocking 行为,使用 loop_start()
或 loop_forever()
。
client = mqtt.Client()
client.connect("localhost", 1883, 60)
client.loop_start()
print date
while True:
try:
fp = open("shared.pkl", 'rb')
shared = pickle.load(fp)
fp.close()
except (EOFError, UnpicklingError):
continue
if date < shared["now"] :
time= shared["time"]
light = shared["light"]
date = shared["now"]
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