我在python脚本下运行,它最终运行shell脚本,该脚本给出了k8s命名空间中运行的版本列表。获得结果,但所需时间>3秒。所以,这导致了"operation_timeout“的松弛。我是python的新手,我有过关于延迟的各种文档,但这并没有帮助,因为这些文档非常复杂。
from subprocess import Popen, PIPE
from subprocess import check_output
from flask import Flask
def get_shell_script_output_using_communicate():
session = subprocess.Popen(['./version.sh'], stdout=PIPE, stderr=PIPE)
stdout, stderr = session.communicate()
if stderr:
raise Exception("Error "+str(stderr))
return stdout.decode('utf-8')
def get_shell_script_output_using_check_output():
stdout = check_output(['./version.sh']).decode('utf-8')
return stdout
app = Flask(__name__)
@app.route('/test',methods=['POST'])
def home():
return '`Version List` ```'+get_shell_script_output_using_check_output()+'```'
app.run(host='0.0.0.0', port=5002, debug=True)有没有办法在命令执行时间超过10秒的情况下获得响应?谢谢!
发布于 2020-03-15 01:58:30
不能将默认超时从Slack增加到斜杠命令。总是3秒。但有可能发送延迟响应长达30分钟。
为此,您需要首先在3秒内作出响应,通过发回HTTP 200OK来确认初始请求。由于这需要您完成当前请求并终止您的主脚本,因此您需要并行运行延迟响应的函数。这可以在进程、线程中,通过调用celery任务或任何其他方式,允许您生成并行运行的python函数。
然后,并行函数可以通过将消息从松弛请求发送到response_url中提供的URL来响应松弛。
下面是一个使用线程的示例实现:
import threading
from time import sleep
from flask import Flask, json, request
import requests
app = Flask(__name__) #create the Flask app
@app.route('/slash', methods=['POST'])
def slash_response():
"""endpoint for receiving all slash command requests from Slack"""
# get the full request from Slack
slack_request = request.form
# starting a new thread for doing the actual processing
x = threading.Thread(
target=some_processing,
args=(slack_request,)
)
x.start()
## respond to Slack with quick message
# and end the main thread for this request
return "Processing information.... please wait"
def some_processing(slack_request):
"""function for doing the actual work in a thread"""
# lets simulate heavy processing by waiting 7 seconds
sleep(7)
# response to Slack after processing is finished
response_url = slack_request["response_url"]
message = {
"text": "We found a result!"
}
res = requests.post(response_url, json=message)
if __name__ == '__main__':
app.run(debug=True, port=8000) #run app in debug mode on port 8000https://stackoverflow.com/questions/60666827
复制相似问题