SleekXMPP和Flask Socketio悬挂线程

时间:2017-03-03 17:54:09

标签: python multithreading flask flask-socketio sleekxmpp

我正在尝试创建可以从浏览器控制的XMPP客户端。我正在使用SleekXMPP作为XMPP客户端和网站烧瓶,所以我可以将信息从XMPP服务器传递到我的XMPP客户端到我的网站前端。如果我使用纯烧瓶,我需要轮询我的服务器以获取更多更新,我宁愿将服务器推送到我的客户端。

我遇到线程问题。连接到服务器工作正常。但是当我断开连接时,我得到一个错误,表明3个线程无法被杀死。如何断开连接时杀死这些线程?或者他们为什么不被杀?或者我应该使用更好的设置?

谢谢!

错误:

DEBUG    Event triggered: session_end
DEBUG    Waiting for 3 threads to exit.
DEBUG    Finished exiting event runner thread after early termination from disconnect() call. 3 threads remain.
DEBUG    Finished exiting send thread after early termination from disconnect() call. 3 threads remain.
DEBUG    Quitting Scheduler thread
DEBUG    Finished exiting scheduler thread after early termination from disconnect() call. 3 threads remain.
ERROR    Hanged threads: [<_MainThread(MainThread, started 140735076622336)>, <Thread(read_thread, started 4452692848)>, <Thread(Thread-1, started daemon 4454843728)>]
ERROR    This may be due to calling disconnect() from a non-threaded event handler. Be sure that event handlers that call disconnect() are registered using: add_event_handler(..., threaded=True)
DEBUG    Event triggered: disconnected
DEBUG     ==== TRANSITION connected -> disconnected

这是我的python脚本。

import eventlet
eventlet.monkey_patch()

from flask import Flask, render_template
from flask_socketio import SocketIO, emit, disconnect
import sleekxmpp
import logging

app = Flask(__name__)
app.debug = True
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)

logging.basicConfig(level=logging.DEBUG, format='%(levelname)-8s %(message)s')


class XMPPClient(sleekxmpp.ClientXMPP):

    def __init__(self, full_jid, pwd):

        self.full_jid = full_jid
        self.pwd = pwd
        sleekxmpp.ClientXMPP.__init__(self, self.full_jid, self.pwd)
        self.HOST = "XXX"
        self.PORT = "XXX"

    def conn(self):
        self.connect(address=(self.HOST, self.PORT))
        self.process(threaded=True)

    def disconn(self):
        self.disconnect(wait=True, send_close=False)

@app.route('/')
def index():
    return render_template('index.html')

@socketio.on('socket_connect', namespace='/vibe')
def _socket_connect():
    xmpp_client.conn()
    return ""

@socketio.on('socket_disconnect', namespace='/vibe')
def _socket_disconnect():
    xmpp_client.disconn()
    return ""


if __name__ == '__main__':
    xmpp_client = XMPPClient(full_jid="XXX", pwd="XXX")
    socketio.run(app)

对于我的前端

<!DOCTYPE HTML>
<html>
<head>
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.3.min.js"></script>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.6/socket.io.min.js"></script>
    <script type="text/javascript" charset="utf-8">
        var socket

        $(document).ready(function(){
            namespace = '/vibe';
            socket = io.connect('http://' + document.domain + ':' + location.port + namespace);
        });

        function handle_connect(cmd) {
            socket.emit(cmd)
        } 
    </script>
</head>
<body>
    <button type="button" onclick="handle_connect('socket_connect')">Connect</button>
    <button type="button" onclick="handle_connect('socket_disconnect')">Disconnect</button>
</body>

</html>

0 个答案:

没有答案