2016-01-12 32 views
0

我的程序從傳感器收集數據並將此數據發送到Web界面,該界面以實時圖形顯示併發回一些命令。如何通過使用python燒瓶socket.io和線程異步獲取來自redis的實時數據?

問題是數據發送有很大的延遲或發送舊數據。我應該改變發送數據異步到兩種方式?

我簡化了我的代碼。例1,當數據來自在幾分鐘內一次:

# System stuff 
import os 
import sys 
import serial 
# Multiprocessing 
from multiprocessing import Process 
from threading import Thread # for same memory 
import timeit 
from time import sleep 
# database 
import redis 
# Web server 
from flask import Flask, render_template 
from flask_socketio import SocketIO, emit, send, join_room, leave_room, close_room, rooms, disconnect 

# Config 
DEBUG = True 


db = redis.StrictRedis(host='localhost', port=6379, db=0) 
#db vars 
db.set('velocity', 0.0) 
db.set('distance', 0.0) 


def webServer(db): 
    app = Flask(__name__) 
    app.config['SECRET_KEY'] = 'secret!' 
    socketio = SocketIO(app) 


    @app.route('/') 
    def index(): 
     return render_template('index.html') 


    def exchangeData(): 
     prev_distance = 0.00 
     while(True): 
      distance = round(float(db.get('distance')), 2) 
      if(distance != prev_distance): 
       velocity = round(float(db.get('velocity')), 2) 
       distance = round(float(db.get('distance')), 2)    
       socketio.emit('exchangeData', { 
        'velocity': velocity, 
        'distance': distance, 
       })    
       prev_distance = distance     
       print("DATA sended: %s m/s" % velocity) 

      sleep(0.2)  

    @socketio.on('connect') 
    def test_connect(): 
     t_exchangeData = Thread(target=exchangeData).start()  

    socketio.run(app, debug=True, host="0.0.0.0") 


def newData(db): 
    c = 0.00 
    while(True): 
     db.set('velocity', c) 
     db.set('distance', c) 
     c += 1.00 
     sleep(1) 




if __name__ == '__main__': 
    p_newData = Process(target=newData, args=(db,)).start() 
    p_webServer = Process(target=webServer, args=(db,)).start() 
    #p_checkConnection = Process(target=checkConnection, args=(db, HOSTNAME, pinglog)).start() 
    #p_calcVD = Process(target=calcVD, args=(db,)).start() 

在瀏覽器中,我得到這個與潛伏期約2分鐘:

Object {velocity: 218, distance: 218} 
// two minutes later 
Object {velocity: 306, distance: 306} 
// two minutes later 
Object {velocity: 306, distance: 306} 

例2,當我不if語句和使用睡眠:

# System stuff 
import os 
import sys 
import serial 
# Multiprocessing 
from multiprocessing import Process 
from threading import Thread # for same memory 
import timeit 
from time import sleep 
# database 
import redis 
# Web server 
from flask import Flask, render_template 
from flask_socketio import SocketIO, emit, send, join_room, leave_room, close_room, rooms, disconnect 

# Config 
DEBUG = True 


db = redis.StrictRedis(host='localhost', port=6379, db=0) 
#db vars 
db.set('velocity', 0.0) 
db.set('distance', 0.0) 


def webServer(db): 
    app = Flask(__name__) 
    app.config['SECRET_KEY'] = 'secret!' 
    socketio = SocketIO(app) 


    @app.route('/') 
    def index(): 
     return render_template('index.html') 


    def exchangeData(): 
     prev_distance = 0.00 
     while(True): 
      #distance = round(float(db.get('distance')), 2) 
      #if(distance != prev_distance): 
      velocity = round(float(db.get('velocity')), 2) 
      distance = round(float(db.get('distance')), 2)    
      socketio.emit('exchangeData', { 
       'velocity': velocity, 
       'distance': distance, 
      })    
      prev_distance = distance     
      print("DATA sended: %s m/s" % velocity) 

      #sleep(0.2)  

    @socketio.on('connect') 
    def test_connect(): 
     t_exchangeData = Thread(target=exchangeData).start()  

    socketio.run(app, debug=True, host="0.0.0.0") 


def newData(db): 
    c = 0.00 
    while(True): 
     db.set('velocity', c) 
     db.set('distance', c) 
     c += 1.00 
     sleep(1) 




if __name__ == '__main__': 
    p_newData = Process(target=newData, args=(db,)).start() 
    p_webServer = Process(target=webServer, args=(db,)).start() 
    #p_checkConnection = Process(target=checkConnection, args=(db, HOSTNAME, pinglog)).start() 
    #p_calcVD = Process(target=calcVD, args=(db,)).start() 

在這種情況下,我得到的實時數據,但它是相同的數據,並改變只有一次在幾分鐘內:

Object {distance: 3, velocity: 3} 
Object {distance: 3, velocity: 3} 
Object {distance: 3, velocity: 3} 
// repeating around 2 minutes 
Object {distance: 357, velocity: 357} 
// repeating again... 

回答

0

主要的問題是,我沒加這兩條線:也

import eventlet 
eventlet.monkey_patch() 

,而我試圖找到解決辦法,犯了一個錯誤,當我打電話從Redis的數據出來while循環。

相關問題