Search code examples
pythonmultithreadingflasksocket.ioeventlet

socketio.emit() doesn't work when interacting using Popen on Windows in a Thread


I think a quick code snippet is better to explain my problem, so please have a look at this:

from flask import Flask
from flask.ext.socketio import SocketIO
from threading import Thread
import subprocess
import threading
from eventlet.green.subprocess import Popen

app = Flask(__name__)
socketio = SocketIO(app)

def get_tasks_and_emit():
    instance = Popen(["tasklist"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=1)

    lines_iterator = iter(instance.stdout.readline, b"")
    data = ""
    for line in lines_iterator:
        data += line.decode("utf8")

    socketio.emit("loaded", data)

    print("::: DEBUG - returned tasks with thread")

@app.route("/")
def index():
    html = "<!DOCTYPE html>"
    html += "<script src=https://code.jquery.com/jquery-2.2.0.min.js></script>"
    html += "<script src=https://cdn.socket.io/socket.io-1.4.5.js></script>"
    html += "<script>"
    html += "var socket = io.connect(window.location.origin);"
    html += "socket.on('loaded', function(data) {alert(data);});"
    html += "function load_tasks_threaded() {$.get('/tasks_threaded');}"
    html += "function load_tasks_nonthreaded() {$.get('/tasks');}"
    html += "</script>"
    html += "<button onclick='load_tasks_nonthreaded()'>Load Tasks</button>"
    html += "<button onclick='load_tasks_threaded()'>Load Tasks (Threaded)</button>"
    return html


@app.route("/tasks")
def tasks():
    get_tasks_and_emit()
    print("::: DEBUG - returned tasks without thread")
    return ""

@app.route("/tasks_threaded")
def tasks_threaded():
    threading.Thread(target=get_tasks_and_emit).start()
    return ""

if __name__ == "__main__":
    socketio.run(app, port=7000, debug=True)

I am running this code on Windows using eventlet, if I don't use eventlet everything is fine (but of course much slower due to the werkzeug threading mode). (And I just checked and it's not working on Linux either)

I hope someone can point me into the right direction. (My Python version is 3.5.1 by the way)


Solution

  • I found the problem. Apparently you have to monkey patch the threading module, so I added

    import eventlet
    eventlet.monkey_patch(thread=True)
    

    and then I also had a problem with long running programs. I had the same problem as the guy in this StackOverflow post: Using Popen in a thread blocks every incoming Flask-SocketIO request

    So I added

    eventlet.sleep()
    

    to the for loop that processes the pipes.

    EDIT: As temoto pointed out, alternatively one can also just use the threading module from eventlet.green like this:

    from eventlet.green import threading