由于响应的产生,瓶子似乎卡住了

时间:2018-02-01 13:26:11

标签: python subprocess bottle

我正在尝试连接到生成响应的后端(其他python脚本)。这需要一点点,似乎它会破坏瓶子的响应。请看这里:

#!/usr/bin/env python
# -*- coding: utf-8 -*-s

from bottle import route, run, response, request
import subprocess

sherlockQueryScriptPath = ".\sherlock-backend\Sherlock_Queries.py"
emptyJsonString = "{}"

def main():
    #print prepareJson("test")
    #sys.exit(0)
    run(host='localhost', port=8080, threaded=True)

def prepareJson(query):
    queryProcess = subprocess.Popen(
        [sherlockQueryScriptPath, '-j', '-q', query],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        shell=True
    )
    queryProcess.wait()                # <== seems to cause problems
    result, error = queryProcess.communicate()

    if error:
        print "Error: ", error
        return emptyJsonString

    return result.strip()

def cors(func):
    def wrapper(*args, **kwargs):
        response.content_type = 'application/json; charset=UTF8'
        response.set_header("Access-Control-Allow-Origin", "*")
        response.set_header("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
        response.set_header("Access-Control-Allow-Headers", "Origin, Content-Type")

        return func(*args, **kwargs)
    return wrapper

@route('/sherlockQuery.json')
@cors
def respondToGet():
    query = request.query.q
    return prepareJson(query)

@route('/sherlockQuery.json', method='POST')
@cors
def respondToPost():
    query = request.forms.get('q')
    return prepareJson(query)

if __name__ == "__main__":
    main()

有人可以告诉我如何开展工作吗?或保持连接直到响应准备好然后发送?

0 个答案:

没有答案