Python 继续执行代码而不中断循环函数的输出
我有一个Python 继续执行代码而不中断循环函数的输出,python,command-line-interface,Python,Command Line Interface,我有一个while循环,以厘米为单位计数,并打印当前步数变量,始终在同一行上 比如说我想跑步 x = True while x is True: pass #printing timer to CLI here print('this is more code running while the timer is still running') input('Press enter to stop the timer') x = False #When x becomes False
while
循环,以厘米为单位计数,并打印当前步数
变量,始终在同一行上
比如说我想跑步
x = True
while x is True:
pass #printing timer to CLI here
print('this is more code running while the timer is still running')
input('Press enter to stop the timer')
x = False
#When x becomes False, I want the while loop to terminate
我知道这一定涉及到子流程或类似的东西,但我不知道自己在学习解决这个问题时应该指向什么方向
以下是供参考的函数:
def timer(stawt, stahp, step, time_step):
from time import sleep
stawt = int(stawt)
stahp = int(stahp)
if stahp < 1:
stahp = 1
elif stahp > 1000:
stahp = 1000
stahp = stahp * 100 + 1
titerator = iter(range(stawt, stahp, step))
while True:
try:
current_step = str(next(titerator))
if int(current_step) < 99:
final_time = '0' + current_step[:0] + '.' + current_step[0:] + 's'
print('\r' + final_time, end='')
elif int(current_step) < 999:
final_time = current_step[:1] + '.' + current_step[1:] + 's'
print('\r' + final_time, end='')
elif int(current_step) < 9999:
final_time = current_step[:2] + '.' + current_step[2:] + 's'
print('\r' + final_time, end='')
else:
final_time = current_step[:3] + '.' + current_step[3:] + 's'
print('\r' + final_time, end='')
sleep(time_step)
except:
print(); break
seconds = int((int(current_step) / 100) % 60)
minutes = int((int(current_step) / 100) // 60)
if minutes < 1:
return ''
else:
final_time_human = str(minutes) + 'm ' + str(round(seconds)) + 's'
print(final_time_human + '\n')
def MAIN():
count_to = float(input('Enter max number of seconds to count:\n'))
print()
timer(0, count_to, 1, 0.01)
MAIN()
def定时器(stawt、stahp、步长、时间步长):
从时间上导入睡眠
stawt=int(stawt)
stahp=int(stahp)
如果stahp<1:
stahp=1
elif stahp>1000:
stahp=1000
stahp=stahp*100+1
滴定仪=iter(范围(stawt、stahp、阶跃))
尽管如此:
尝试:
当前步骤=str(下一步(滴定仪))
如果int(当前步进)<99:
最终_时间='0'+当前_步[:0]+'.+当前_步[0:]+'s'
打印('\r'+最终时间,结束='')
elif int(当前步进)<999:
最终_时间=当前_步骤[:1]+'.+当前_步骤[1::]+'s'
打印('\r'+最终时间,结束='')
elif int(当前步进)<9999:
最终_时间=当前_步[:2]+'.+当前_步[2::]+'s'
打印('\r'+最终时间,结束='')
其他:
最终_时间=当前_步骤[:3]+'.+当前_步骤[3::]+'s'
打印('\r'+最终时间,结束='')
睡眠(时间步长)
除:
打印();打破
秒=整数((整数(当前步长)/100)%60)
分钟数=int((int(当前步数)/100)//60)
如果分钟数小于1:
返回“”
其他:
最终时间人类=str(分钟)+“m”+str(轮(秒))+“s”
打印(最终时间人类+“\n”)
def MAIN():
count_to=float(输入('输入要计数的最大秒数:\n'))
打印()
计时器(0,计数到,1,0.01)
MAIN()
您需要使用线程
导入线程
x=真
def thread_函数():
虽然x是真的:
在此处将#打印计时器传递给CLI
threading.Thread(target=Thread\u function.start())
#继续执行您要执行的其他步骤
# ...
#这将终止计时器循环
x=假
Python线程文档:
如果要始终在同一行打印时间,则需要控制终端光标。欲了解更多信息,请登录:多亏了@dorukerenaktas,我已经完成了这项工作。这是他们在我的脚本中的答案:
import threading
from os import system
timer_thread = None
def timer(stawt, stahp, step, time_step):
from time import sleep
global run_timer
stawt = int(stawt)
stahp = int(stahp)
if stahp < 1:
print('Sorry, I limit min count to 1 second!\n')
stahp = 1
elif stahp > 1000:
print('Sorry, I limit max count to 1000 seconds!\n')
stahp = 1000
else:
print()
stahp = stahp * 100 + 1
titerator = iter(range(stawt, stahp, step))
def print_time():
while run_timer is True:
try:
current_step = str(next(titerator))
if int(current_step) < 99:
final_time = '0' + current_step[:0] + '.' + current_step[0:] + 's'
print('\r' + final_time, end='')
elif int(current_step) < 999:
final_time = current_step[:1] + '.' + current_step[1:] + 's'
print('\r' + final_time, end='')
elif int(current_step) < 9999:
final_time = current_step[:2] + '.' + current_step[2:] + 's'
print('\r' + final_time, end='')
else:
final_time = current_step[:3] + '.' + current_step[3:] + 's'
print('\r' + final_time, end='')
sleep(time_step)
except:
break
seconds = int((int(current_step) / 100) % 60)
minutes = int((int(current_step) / 100) // 60)
if minutes < 1:
return ''
else:
final_time_human = str(minutes) + 'm ' + str(round(seconds)) + 's'
print('\n' + final_time_human)
print_time()
def _init_timer():
global run_timer; run_timer = True
global timer_thread
print('Enter max number of seconds to count: ', end='')
count_to = float(input())
timer_thread = threading.Thread(target=timer, args=(0, count_to, 1, 0.01))
timer_thread.start()
print('\rPress enter to stop the timer:')
usr_input = input(); run_timer = False
system('clear')
_init_timer()
timer_thread.join()
print('\nGoodbye!')
导入线程
从操作系统导入系统
计时器线程=无
def定时器(stawt、stahp、步长、时间步长):
从时间上导入睡眠
全局运行计时器
stawt=int(stawt)
stahp=int(stahp)
如果stahp<1:
打印('对不起,我将最小计数限制为1秒!\n')
stahp=1
elif stahp>1000:
打印('对不起,我将最大计数限制为1000秒!\n')
stahp=1000
其他:
打印()
stahp=stahp*100+1
滴定仪=iter(范围(stawt、stahp、阶跃))
def print_time():
运行计时器为真时:
尝试:
当前步骤=str(下一步(滴定仪))
如果int(当前步进)<99:
最终_时间='0'+当前_步[:0]+'.+当前_步[0:]+'s'
打印('\r'+最终时间,结束='')
elif int(当前步进)<999:
最终_时间=当前_步骤[:1]+'.+当前_步骤[1::]+'s'
打印('\r'+最终时间,结束='')
elif int(当前步进)<9999:
最终_时间=当前_步[:2]+'.+当前_步[2::]+'s'
打印('\r'+最终时间,结束='')
其他:
最终_时间=当前_步骤[:3]+'.+当前_步骤[3::]+'s'
打印('\r'+最终时间,结束='')
睡眠(时间步长)
除:
打破
秒=整数((整数(当前步长)/100)%60)
分钟数=int((int(当前步数)/100)//60)
如果分钟数小于1:
返回“”
其他:
最终时间人类=str(分钟)+“m”+str(轮(秒))+“s”
打印('\n'+最终时间)
打印时间()
def_init_timer():
全局运行计时器;运行计时器=真
全局定时器线程
打印('输入要计数的最大秒数:',结束='')
count_to=浮点(输入())
timer\u thread=threading.thread(target=timer,args=(0,count\u to,1,0.01))
timer_thread.start()
打印('\r按enter停止计时器:')
usr_输入=输入();运行计时器=错误
系统(“清除”)
_初始化计时器()
timer_thread.join()
打印('\nGoodbye!')
您只想在同一行上打印?或者你想运行异步?我相信是异步的。计时器不断地用当前的_步骤变量擦除它上次打印的行。在循环过程中,我希望其他命令同时出现;我希望这些命令能够将stdout和stderr打印到shell中,而不会中断每10毫秒重新打印一次的计时器行。如果这意味着需要使用多个线程,我希望这些线程共享原始问题顶部的x布尔值。