开发者

On enter action?

开发者 https://www.devze.com 2023-02-19 19:33 出处:网络
Hey I\'m working on a python project that requires an action that takes a couple minutes. The thing is since it takes a couple minutes I\'d like the user to be able to press enter to see the current s

Hey I'm working on a python project that requires an action that takes a couple minutes. The thing is since it takes a couple minutes I'd like the user to be able to press enter to see the current status of the action. How can I do this in Pyth开发者_高级运维on 2?


@Space_C0wb0y is right, a progress bar is a good solution. However, this demonstrates one way to do what you asked for. Some code pinched from here: Non-blocking read on a subprocess.PIPE in python

import fcntl, os, sys

# make stdin a non-blocking file
fd = sys.stdin.fileno()
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)

def enter_pressed():
    data = sys.stdin.read(1)
    return bool(data)

i = 0
while True:
    i += 1
    if enter_pressed():
        print(i)
0

精彩评论

暂无评论...
验证码 换一张
取 消