I'm trying to create and XMPP client that I can control from my browser. I'm using SleekXMPP for the XMPP client and flask-socketio for the website so I can relay information from the XMPP server to my XMPP client to my website frontend. If I used pure flask, I would need to poll my server for additional updates which I would rather have the server push to my client.
I am having some issues with threads. Connecting to the server works fine. But when I disconnect, I get an error showing that 3 threads cannot be killed. How can I kill these threads on disconnect? Or why are they not being killed? Or is there a better setup I should be using?
Thanks!!
The error:
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
Here is my python script.
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)
And for my frontend
<!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>