如何通过使用python flask socket.io和线程来异步从redis获取实时数据?

时间:2016-01-12 05:42:27

标签: python multithreading redis socket.io multiprocessing

我的程序从传感器收集数据并将此数据发送到Web界面,该界面以实时图形显示并发回一些命令。

问题是数据发送有很大的延迟或发送旧数据。我应该改变什么来向两种方式发送数据异步?

我简化了我的代码。例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语句和sleep时:

# 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...

1 个答案:

答案 0 :(得分:0)

主要问题是我没有添加这两行:

import eventlet
eventlet.monkey_patch()

另外,当我试图找到解决方案时,当我从redis调用while循环中的数据时出错了。