代码之家  ›  专栏  ›  技术社区  ›  Milan Velebit

烧瓶SocketIO是否从不同模块发出不工作?

  •  0
  • Milan Velebit  · 技术社区  · 7 年前

    当我召唤 socket.emit('someevent','blahblah') 从server.py文件中,一切都按预期工作。但是当我试图从 机器人.py ,什么也没发生。

    代码:

    服务器.py:

    import eventlet
    eventlet.monkey_patch()
    import eventlet.wsgi
    from flask import Flask, render_template, jsonify, request, abort
    from flask_cors import CORS, cross_origin
    import threading
    from thread_manager import ThreadManager
    from flask_socketio import SocketIO, emit, send
    
    cho = Flask(__name__, static_folder="client/dist", template_folder="client/dist")
    socketio = SocketIO(cho)
    cors = CORS(cho)
    
    threadmanager = ThreadManager()     # Start the thread manager
    
    import bot as bot_module
    
    @cho.route('/api/start_bot', methods=['POST'])
    @cross_origin()
    def startBot():
        """
        Begins the execution
        :return:
        """
        if request.method == 'POST':
            request_json = request.get_json()
            .... more code 
            bot = bot_module.Bot(some_args_from_request_above)
            bot_thread = threading.Thread(target=bot.run)
            bot_thread.start()
            if threadmanager.check_thread_status(bot_name):
                print('Thread is alive!')
                return ok_res
            else:
                print('Thread seems inactive')
                return bad_res
    
    if __name__ == "__main__":
        eventlet.wsgi.server(eventlet.listen(('0.0.0.0', 5000)), cho, debug=True)
    

    机器人.py

    import server
    class Bot: 
     .....
        def run(self):
            server.socketio.emit('someevent', 'w0w') # <-- nothing happens
    

    我知道我正在使用标准的线程机制,但它似乎与线程无关,因为我可以在Bot类中创建一个随机静态方法,在从主文件创建单独的线程之前调用它,什么都不会发生。thread_manager模块不包含任何会干扰的内容,但我甚至已经将其从图片中完全删除,并且没有任何更改。有线索吗?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Milan Velebit    7 年前

    原来这和循环导入完全相关。从入口点分离应用程序声明是可行的,这样我就有了第三个要导入的引用文件 短袜 从。