Python 烧瓶插座.io不';当从线程发出信号时不工作 描述

Python 烧瓶插座.io不';当从线程发出信号时不工作 描述,python,multithreading,flask,socket.io,Python,Multithreading,Flask,Socket.io,客户端有2个按钮: 一种是让服务器定期发送消息 另一个停止发送定期消息 这个问题代表了我试图解决的真正问题 我构建了这个应用程序,在服务器端它似乎可以工作,但是客户端没有收到服务器推送,但是能够启动推送并杀死它 我试过的 服务器端 import openSocket from 'socket.io-client'; import React, { Component } from 'react'; class Test extends Component { state = {

客户端有2个按钮:

  • 一种是让服务器定期发送消息
  • 另一个停止发送定期消息
这个问题代表了我试图解决的真正问题

我构建了这个应用程序,在服务器端它似乎可以工作,但是客户端没有收到服务器推送,但是能够启动推送并杀死它

我试过的 服务器端

import openSocket from 'socket.io-client';
import React, { Component } from 'react';

class Test extends Component {
  state = {
    pushedFromServer: [],
    socket: null
  };

  componentDidMount() {
    const url = 'localhost:8082/test';
    const socket = openSocket(url);
    socket.on('connect', () => console.log('Test connected!!'));
    socket.on('disconnect', () => console.log('Test disconnected!!'));
    socket.on('serverResponse', response => {
      console.log(response);
      const pushedFromServer = [...this.state.pushedFromServer];
      pushedFromServer.push(response);
      this.setState({ pushedFromServer });
    });
    this.setState({ socket });
  }

  killServerPush = () => {
    this.state.socket.emit('killServerPush');
  };

  startServerPush = () => {
    this.state.socket.emit('startServerPush');
  };

  render() {
    return (
      <div>
        <button onClick={this.startServerPush}>
          <h3>Start push from server</h3>
        </button>
        <button onClick={this.killServerPush}>
          <h3>Kill push from server</h3>
        </button>
        <ul>
          {this.state.pushedFromServer.map(value => (
            <li>{value}</li>
          ))}
        </ul>
      </div>
    );
  }
}

export default Test;
随机导入
从线程导入线程
从时间上导入睡眠
从烧瓶进口烧瓶
从flask_socketio导入socketio
SOCKET_命名空间=“/test”
这是假的吗
app=烧瓶(名称)
app.config['SECRET\u KEY']='SECRET!!'
socketio=socketio(应用程序)
def服务器推送(fps):
dt=1/fps
全球化正在推动
当你在推的时候:
使用app.test_request_上下文('/'):
sent=f“已推送服务器!!{random.random()}”
打印(已发送)
emit(“serverResponse”,已发送,名称空间=SOCKET\u名称空间)
睡眠(dt)
@on('connect',namespace=SOCKET\u namespace)
_connect()上的def:
打印(“已连接的服务器!!”)
emit(“serverResponse”,“First Push”,namespace=SOCKET\u namespace)
@socketio.on('disconnect',namespace=SOCKET\u namespace)
def on_disconnect():
打印(“断开连接的服务器!!”)
@on('startServerPush',namespace=SOCKET\u namespace)
启动服务器推送时的def(fps=1):
打印(“服务器推送启动!!”)
全球化正在推动
是真的吗
emit(“serverResponse”,“Start Push”,namespace=SOCKET\u namespace)
线程(target=lambda:server_push(fps)).start()
@on(“killServerPush”,名称空间=SOCKET\u名称空间)
_kill_server_push()上的def:
打印(“服务器推送停止!!”)
全球化正在推动
这是假的吗
emit(“serverResponse”,“Kill Push”,namespace=SOCKET\u namespace)
def main():
运行(应用程序,端口=8082,调试=True)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()
客户端

import openSocket from 'socket.io-client';
import React, { Component } from 'react';

class Test extends Component {
  state = {
    pushedFromServer: [],
    socket: null
  };

  componentDidMount() {
    const url = 'localhost:8082/test';
    const socket = openSocket(url);
    socket.on('connect', () => console.log('Test connected!!'));
    socket.on('disconnect', () => console.log('Test disconnected!!'));
    socket.on('serverResponse', response => {
      console.log(response);
      const pushedFromServer = [...this.state.pushedFromServer];
      pushedFromServer.push(response);
      this.setState({ pushedFromServer });
    });
    this.setState({ socket });
  }

  killServerPush = () => {
    this.state.socket.emit('killServerPush');
  };

  startServerPush = () => {
    this.state.socket.emit('startServerPush');
  };

  render() {
    return (
      <div>
        <button onClick={this.startServerPush}>
          <h3>Start push from server</h3>
        </button>
        <button onClick={this.killServerPush}>
          <h3>Kill push from server</h3>
        </button>
        <ul>
          {this.state.pushedFromServer.map(value => (
            <li>{value}</li>
          ))}
        </ul>
      </div>
    );
  }
}

export default Test;
从“socket.io客户端”导入openSocket;
从“React”导入React,{Component};
类测试扩展了组件{
状态={
pushedFromServer:[],
套接字:空
};
componentDidMount(){
constURL='localhost:8082/test';
const socket=openSocket(url);
socket.on('connect',()=>console.log('testconnected!!');
socket.on('disconnect',()=>console.log('testdisconnected!!');
socket.on('serverResponse',response=>{
控制台日志(响应);
const pushedFromServer=[…this.state.pushedFromServer];
pushedFromServer.push(响应);
this.setState({pushedFromServer});
});
this.setState({socket});
}
killServerPush=()=>{
this.state.socket.emit('killServerPush');
};
startServerPush=()=>{
this.state.socket.emit('startServerPush');
};
render(){
返回(
从服务器启动推送
从服务器终止推送
    {this.state.pushedFromServer.map(值=>(
  • {value}
  • ))}
); } } 导出默认测试;
最后说明 在客户端中,我可以接收第一次推送,以及启动推送,我还可以停止客户端的定期进程并重新启动它我无法在客户端上接收定期消息

谢谢

通过查看,我找到了问题的解决方案

只需要更改服务器端

换行:

Thread(target=lambda: server_push(fps)).start()

不要使用python睡眠,而是使用:

socketio.sleep(dt)