在线程中使用Windows上的Popen进行交互时,socketio.emit()不起作用

时间:2016-02-06 00:29:22

标签: python multithreading flask socket.io eventlet

我认为快速的代码段更能解释我的问题,所以请看一下:

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)

我在Windows上使用eventlet运行此代码,如果我不使用eventlet,一切都很好(但当然由于werkzeug线程模式而慢得多)。 (我刚检查过,它也没有在Linux上工作)

我希望有人能指出我正确的方向。 (顺便说一下,我的Python版本是3.5.1)

1 个答案:

答案 0 :(得分:3)

我发现了问题。显然你必须修补线程模块,所以我添加了

import eventlet
eventlet.monkey_patch(thread=True)

然后我也遇到了长时间运行程序的问题。我遇到了与StackOverflow帖子中的人一样的问题: Using Popen in a thread blocks every incoming Flask-SocketIO request

所以我添加了

eventlet.sleep()

到处理管道的for循环。

编辑: 正如temoto指出的那样,也可以使用eventlet.green中的线程模块,如下所示:

from eventlet.green import threading