Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: 更新python脚本默认模板,提供清楚io缓存配置 #2350

Closed
nekzhang opened this issue Aug 21, 2023 · 0 comments
Closed

perf: 更新python脚本默认模板,提供清楚io缓存配置 #2350

nekzhang opened this issue Aug 21, 2023 · 0 comments
Assignees
Labels
done 已上线到正式环境并验收通过 kind/enhancement 功能改进特性 tested 已经在测试环境测试通过

Comments

@nekzhang
Copy link
Collaborator

原python脚本默认模板:

#!/usr/bin/env python
# -*- coding: utf8 -*-

import datetime
import os
import sys

def _now(format="%Y-%m-%d %H:%M:%S"):
    return datetime.datetime.now().strftime(format)

##### 可在脚本开始运行时调用,打印当时的时间戳及PID。
def job_start():
    print("[%s][PID:%s] job_start" % (_now(), os.getpid()))

##### 可在脚本执行成功的逻辑分支处调用,打印当时的时间戳及PID。 
def job_success(msg):
    print("[%s][PID:%s] job_success:[%s]" % (_now(), os.getpid(), msg))
    sys.exit(0)

##### 可在脚本执行失败的逻辑分支处调用,打印当时的时间戳及PID。
def job_fail(msg):
    print("[%s][PID:%s] job_fail:[%s]" % (_now(), os.getpid(), msg))
    sys.exit(1)

if __name__ == '__main__':

    job_start()

###### iJobs中执行脚本成功和失败的标准只取决于脚本最后一条执行语句的返回值
###### 如果返回值为0,则认为此脚本执行成功,如果非0,则认为脚本执行失败
###### 可在此处开始编写您的脚本逻辑代码


更改后:(中文)

#!/usr/bin/env python
# -*- coding: utf8 -*-

import datetime
import os
import sys

def _now(format="%Y-%m-%d %H:%M:%S"):
    return datetime.datetime.now().strftime(format)

##### 可在脚本开始运行时调用,打印当时的时间戳及PID。
def job_start():
    print("[%s][PID:%s] job_start" % (_now(), os.getpid()))
    sys.stdout.flush()

##### 可在脚本执行成功的逻辑分支处调用,打印当时的时间戳及PID。 
def job_success(msg):
    print("[%s][PID:%s] job_success:[%s]" % (_now(), os.getpid(), msg))
    sys.stdout.flush()
    sys.exit(0)

##### 可在脚本执行失败的逻辑分支处调用,打印当时的时间戳及PID。
def job_fail(msg):
    print("[%s][PID:%s] job_fail:[%s]" % (_now(), os.getpid(), msg))
    sys.stdout.flush()
    sys.exit(1)

if __name__ == '__main__':

    job_start()

###### 脚本执行成功和失败的标准只取决于最后一段执行语句的返回值
###### 若返回值为0,则认为此脚本执行成功,反之(非0)则认为脚本执行失败
###### Python脚本为了避免因长时间未刷新缓冲区导致标准输出异常,
###### 建议在print的下一行使用 sys.stdout.flush() 来强制将被缓存的输出信息刷新到控制台上
###### 可在此处开始编写您的脚本逻辑代码


更改后:(英文)

#!/usr/bin/env python
# -*- coding: utf8 -*-

import datetime
import os
import sys

def _now(format="%Y-%m-%d %H:%M:%S"):
    return datetime.datetime.now().strftime(format)

##### It's usually called when the script starts to run, prints the timestamp and PID.
def job_start():
    print("[%s][PID:%s] job_start" % (_now(), os.getpid()))
    sys.stdout.flush()

##### Use this func. to combine the execution results of multiple hosts and display by group.
def job_success(msg):
    print("[%s][PID:%s] job_success:[%s]" % (_now(), os.getpid(), msg))
    sys.stdout.flush()
    sys.exit(0)

##### Use this func. to combine the execution results of multiple hosts and display by group.
def job_fail(msg):
    print("[%s][PID:%s] job_fail:[%s]" % (_now(), os.getpid(), msg))
    sys.stdout.flush()
    sys.exit(1)

if __name__ == '__main__':

    job_start()

###### The script execution result is depends on the value of return/exit code, weather it success or failed.
###### If return code is 0, means success, otherwise failed.
###### In order to avoid abnormal standard output due to the buffer not being refreshed for a long time in Python scripts, 
###### we recommended to use sys.stdout.flush() in the line following the print statement to forcibly flush the cached output to the console.
###### Start to writing your script code below this line.


变化:

  1. job_start job_successjob_fail 三个内置函数中的print后面增加了 sys.stdout.flush() 方法,避免因io缓存导致输出堵塞
  2. 在底部的注释中也新增了2行来提醒用户注意及时刷新io缓冲区
@nekzhang nekzhang added kind/enhancement 功能改进特性 backlog 需求初始状态,等待产品进行评估 labels Aug 21, 2023
@bkjob-bot bkjob-bot added for test 可以在测试环境进行验收 and removed backlog 需求初始状态,等待产品进行评估 labels Aug 28, 2023
@fabio-he fabio-he added the tested 已经在测试环境测试通过 label Aug 29, 2023
@bkjob-bot bkjob-bot added done 已上线到正式环境并验收通过 and removed for test 可以在测试环境进行验收 tested 已经在测试环境测试通过 labels Aug 29, 2023
@jsonwan jsonwan added the tested 已经在测试环境测试通过 label Aug 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
done 已上线到正式环境并验收通过 kind/enhancement 功能改进特性 tested 已经在测试环境测试通过
Projects
None yet
Development

No branches or pull requests

5 participants