这是我试图实现的代码
from flask import Flask, request, jsonify
import json
import pythoncom
import win32com.client
from win32com.client import Dispatch
xl = None
xl = win32com.client.Dispatch("Excel.Application")
app = Flask(__name__)
@app.route("/check", methods=['POST'])
def check():
pythoncom.CoInitialize()
if request.method == 'POST':
data = request.get_json()
fname = data['fname']
phName = data['PH_Name']
liName = data['LI_NAME']
ppm = data['PPM']
policyTerm = data['Policy_Term']
sumAssured = data['Sum_Assured']
wb = None
if xl :
wb = xl.Workbooks.Open(fname)
inp_sheet = wb.Sheets["Inputs"]
#read data from sheet
data = inp_sheet.Range('D8').Value
wb.Close(False)
xl.Quit()
return (data)
if __name__ == '__main__':
app.run(threaded=True, port=5001, debug=True)
问题是,每当使用烧瓶函数打开工作簿时,win32com 都会创建新线程,因此需要将 Dispatcher 对象编组到流中以将其传递给函数。我如何为 api 调用做到这一点?
我发现了一些使用 CoMarshalInterThreadInterfaceInStream 和 CoGetInterfaceAndReleaseStream 的建议,但是当对 api 进行多个并行调用时它们无法运行。