【发布时间】:2018-08-06 19:23:41
【问题描述】:
当我从 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 模块不包含任何会干扰的内容,但我什至将它从图片中完全删除,并且没有任何改变。有什么线索吗?
【问题讨论】: