【问题标题】:Increase timeout from slack slash command in python "operation_timeout"在python“operation_timeout”中增加slack slash命令的超时时间
【发布时间】:2020-03-13 08:01:25
【问题描述】:

我在 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 秒,有没有办法获得响应?谢谢!

【问题讨论】:

    标签: python shell kubernetes slack slack-api


    【解决方案1】:

    无法将 Slack 的默认超时时间增加到斜线命令。始终是 3 秒。但是可以发送长达 30 分钟的延迟响应。

    为此,您需要在 3 秒内首先响应,通过发回 HTTP 200 OK 来确认初始请求。由于这需要您完成当前请求并终止主脚本,因此您需要并行运行延迟响应的函数。这可以在进程、线程中,通过调用 celery 任务或任何其他允许您生成并行运行的 python 函数的方式。

    然后,并行函数可以通过将消息从 Slack 请求发送到response_url 中提供的 URL 来响应 Slack。

    这是一个使用线程的示例实现:

    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 8000
    

    【讨论】:

    • 如果没有thread.join,如何确保线程在所需操作结束后立即终止?
    • @zcahfg2 线程完成工作后将自动终止。如果你想等待线程完成然后做其他事情,你可以加入一个连接。但我认为在这种情况下不需要。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 2016-08-11
    • 1970-01-01
    相关资源
    最近更新 更多