简体   繁体   中英

Client-server refresh dependent on time

I have a program written where the client side is in JavaScript (React) and the server side is written in Python using Flask. I want the client side to refresh data every 30 seconds but I want the server side to refresh at different paces (i want Route A to refresh every 30 seconds, Route B to refresh every 60 seconds) By Route I am referring to Python-Flask Routes.

So far in my client side (as far as this refreshing code goes) I have

refreshData() {
        request.get("/cData")
        .end((err,res) => {
            if (err) {
                console.log("error cdata request: " + err);
                return;
            }
            this.setState({cTimes: res.body});
        });

        request.get("/mData")
        .end((err,res) => {
            if (err) {
                console.log("error mdata request: " + err);
                return;
            }
            this.setState({mTimes: res.body})
        });
    }
    componentDidMount() {
        this.refreshData();
        setInterval(this.refreshData(), 30000);
    }

A route in the backend looks like this

@app.route("/cData")
def cData():
   #do work
   return jsonify(work)

The return data of each flask route will be in JSON form (dictionary)

Any idea what I need to do on the client side (React) to really get this done? Rather than using the threading module in Python (since that does not allow multiple threads to run at the same time) is there a different way? Multi-processing?

您可以尝试使用setInterval命令,可以在w3school上查找如何使用它,也可以尝试(此为我的建议)在项目中使用flask -socketio,并通过flask socetio可以在客户端和服务器之间实时传输数据

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM