Python -检查是否在给定超时内满足条件

jgovgodb  于 2023-03-21  发布在  Python
关注(0)|答案(1)|浏览(166)

我有一个脚本,1.下载并推送二进制文件到内部注册表(func_to_push_image)和2.处理消息队列的一部分消息,通过过滤关键字的消息来查看推送是否成功3.如果推送成功,监视并处理消息队列的另一部分,通过查看install_status关键字来验证这两个二进制文件是否安装。
推送操作的超时时间为2分钟,否则脚本立即失败。安装操作的超时时间为1分钟,否则脚本超时失败。
二进制文件安装的消息队列输出如下所示。

{
"location": "root",
"md5":"a3288ec45c8d159fbb656972eb4cdfg84jsfbsdf",
"fireEye":"v3",
"Binaries":[
A:{
"name":"HP-Protect",
"version": "v1.3",
"effect": "NIL",
"install_status":"On Going",
}
B:{
"name":"Covertur",
"version": "v1.0",
"effect": "NIL",
"install_status":"Installed"
}]
}

编辑代码:

registry_timeout = 2
install_timeout = 1
Other variables holding server and partition details.

def get_info_from_messaging_queue(server, broker):
    depending on the partition, returns the messages from the messaging queue.

def func_to_push_image():
    logic to upload the artifact to the registry.

def func_to_check_status_of_installed_binaries():  #This function should Ideally check the status of both binaries has been "Completed"
    status = False
    install_status = get_info_from_messaging_queue(server, broker)
    if install_status['Binaries'][A]['install_status'] == "Completed" and install_status['Binaries'][B]['install_status'] == "Completed":
            status = True
    return status
    
    
# to check if image is pushed within 2 minutes.
def timeout_verify_push_status():
    start = datetime.now()
    stop = start + timedelta(minutes=registry_timeout)
    while True:
        push_status = func_to_push_image()
        if push_status:
            break
        start = datetime.now()
        if start > stop_time:
            print("Timed Out while pushing image to registry...")
            break

# to check if installation is complete within a minute..
def timeout_verify_installation_status()():
    start = datetime.now()
    stop = start + timedelta(minutes=install_timeout)
    while True:
        install_status = func_to_check_status_of_installed_binaries()
        if install_status:
            break
        start_time = datetime.now()
        if start_time > stop_time:
            print("Timeout while installtion of Binaries...")
            break

if __name__ == '__main__':
    timeout_verify_push_status()
    timeout_verify_installation_status()

这可以完成任务,如果状态不是在一分钟内完成,它就会超时,但是效率不高,我试图使用一个函数来处理超时条件,但是失败了。

sirbozc5

sirbozc51#

你可以使用threadPoolExecutor或processPoolExecutor:

registry_timeout = 2
    install_timeout = 1
    Other variables holding server and partition details.
    
    def get_info_from_messaging_queue(server, broker):
        depending on the partition, returns the messages from the messaging queue.
    
    def func_to_push_image():
        logic to upload the artifact to the registry.
    
    def func_to_check_status_of_installed_binaries():  #This function should Ideally check the status of both binaries has been "Completed"
        status = False
        install_status = get_info_from_messaging_queue(server, broker)
        if install_status['Binaries'][A]['install_status'] == "Completed" and install_status['Binaries'][B]['install_status'] == "Completed":
                status = True
        return status

    if __name__ == '__main__':
        with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
            push_task = execuor.submit(func_to_push_image)
            install_task = executor.submit(func_to_check_status_of_installed_binaries)
            push_status = push_task.result(timeout=120)
            install_status = install_task.result(timeout=120)

相关问题