当我从server.py文件调用socket.emit('someevent','blahblah')
时,一切都会按预期进行。但是,当我尝试从 bot.py 调用相同的方法时,什么也没发生。
代码:
server.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)
bot.py
import server
class Bot:
.....
def run(self):
server.socketio.emit('someevent', 'w0w') # <-- nothing happens
我知道我正在使用标准的线程机制,但是它似乎与线程无关,因为我可以在Bot类中创建一个随机的静态方法,在从主文件创建单独的线程之前调用它,并且没有任何结果发生。 thread_manager模块不包含任何会干扰的内容,但我什至已将其从图片中完全删除,并且没有任何更改。有什么线索吗?
答案 0 :(得分:0)
事实证明,这与循环导入完全相关。从入口点拆分应用程序声明是可行的,这样我就有了第三个参考文件,可从中导入 socketio 。