python强制git钩子服务器端实时输出在同一行上 [英] python Force git hook server side output on same line in realtime

查看:191
本文介绍了python强制git钩子服务器端实时输出在同一行上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

git hooks服务器端输出只能在换行符上传输。我想配置一个自定义任务并在同一行上打印实时输出。我怎样才能做到这一点?我尝试了sys.stdout.flush,但是一旦完成就传输一切。它可以实时工作,如果我添加一个换行符。
我想要的东西就像


步骤A)开始......完成

在给定时间后附加每个'。'。



我现在的代码如下所示,它的输出

  import sys,time,os 


def print_realtime():
sys.stdout.write('Started。')
sys.stdout.flush()
time.sleep(1)
for i in range(1) ,15):
sys.stdout.write('。')
sys.stdout.flush()
time.sleep(0.5)

if __name__ = =__main__:
print_realtime()

然而,如果我追加'\\\
':

pre $ sys $ stdout.write('。\ n')
code $ <$ pre

解决方案

问题归结为Git本身不打印累计 remote:文本直到t他远程发送换行符或回车



因此,如果我们将内部循环更改为:

  (1,15):
sys.stdout.write('。'* i +'\r')
sys.stdout.flush()
time.sleep(0.5 )

你会看到你想要的效果。

请注意 \ r 将打印位置移回到 remote:之后,所以我们必须重复我们迄今为止打印的内容(因此需要先打印,然后两个 s),然后三个,等等)。

git hooks server side output only transmits on a newline. I want to configure a custom task and print real-time output on the same line. How can I achieve that? I tried sys.stdout.flush but it transmits everything once complete. It does work in real-time if I add a newline. I want something like

Step A)Started......Completed

with each '.' appended after a given time.

My current code looks like the following and it outputs only when the method is completed.

import sys, time, os


def print_realtime():
    sys.stdout.write('Started.')
    sys.stdout.flush()
    time.sleep(1)
    for i in range(1, 15):
        sys.stdout.write('.')
        sys.stdout.flush()
        time.sleep(0.5)

if __name__ == "__main__":
    print_realtime()

However, it works in realtime if I append '\n' like:

sys.stdout.write('.\n')

解决方案

The problem comes down to the fact that Git itself does not print accumulated remote: text until the remote sends either a newline or carriage return.

Hence, if we change your inner loop to read:

for i in range(1, 15):
    sys.stdout.write('.' * i + '\r')
    sys.stdout.flush()
    time.sleep(0.5)

you will see the effect you desire.

Note that the \r moves the print position back to just after the word remote:, so we must repeat what we have printed so far (hence the need to print first one ., then two .s, then three, and so on).

这篇关于python强制git钩子服务器端实时输出在同一行上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆