Return 对象结束并行 while True 循环
Return object to end parallel while True loop
我正在写一些 API 以使我的 python 命令行输出看起来更漂亮一点。但是仍然有一个大问题,因为我希望能够调用像 display_loading_until_event(text)
这样的函数,它应该:
- 立即return一种“完成”加载动画的方法
- 每 0.25 秒更新一次加载动画。
我也尝试使用 multiprocessing
库中的 Process
,但它似乎不起作用:
from time import sleep
def display_loading_until_event(message):
from multiprocessing import Process
class Stop:
def __init__(self):
self.run = True
class LoadingAnimationProcess(Process):
def __init__(self, stopper, *args, **kwargs):
Process.__init__(self, *args, **kwargs)
self.stop = stopper
def run(self):
def get_loading(position):
return positions[position] # Returns the way the loading animation looks like (position: ["\", "|", "-"])
print(get_loading(0), end=message) # Simplified version
pos = 0
while self.stop.run:
sleep(0.25)
pos = pos + 1
print("\r" + get_loading(pos) + message, end="") # Again, simplified
if pos == len(positions) - 1:
pos = -1
sleep(0.25)
print("\r" + "✔" + message) # Prints ✔ to signal that the loading is done
stopper = Stop()
loading = LoadingAnimationProcess(stopper)
loading.start() # Starts the process
def stop():
stopper.run = False
sleep(1)
# loading.join() ???
return stop # This therefore returns a function to stop the loading, right?
# So this should in theory work:
positions = ["\", "|", "-"]
endfnc = display_loading_until_event("Hello loading")
sleep(4)
endfnc()
# And now the loading SHOULD STOP
此代码只是一个示例,实际代码稍微复杂一些,但应该可以。
加载动画在 powershell 上不起作用(\r
是破坏它的东西。)
当前代码有效,display_loading_until_event
调用后的所有内容都已执行,但加载并未停止。
而且我认为这不是正确的方法...
这种工作:
我只是 return “condition.set” 方法,它起作用了!
我正在写一些 API 以使我的 python 命令行输出看起来更漂亮一点。但是仍然有一个大问题,因为我希望能够调用像 display_loading_until_event(text)
这样的函数,它应该:
- 立即return一种“完成”加载动画的方法
- 每 0.25 秒更新一次加载动画。
我也尝试使用 multiprocessing
库中的 Process
,但它似乎不起作用:
from time import sleep
def display_loading_until_event(message):
from multiprocessing import Process
class Stop:
def __init__(self):
self.run = True
class LoadingAnimationProcess(Process):
def __init__(self, stopper, *args, **kwargs):
Process.__init__(self, *args, **kwargs)
self.stop = stopper
def run(self):
def get_loading(position):
return positions[position] # Returns the way the loading animation looks like (position: ["\", "|", "-"])
print(get_loading(0), end=message) # Simplified version
pos = 0
while self.stop.run:
sleep(0.25)
pos = pos + 1
print("\r" + get_loading(pos) + message, end="") # Again, simplified
if pos == len(positions) - 1:
pos = -1
sleep(0.25)
print("\r" + "✔" + message) # Prints ✔ to signal that the loading is done
stopper = Stop()
loading = LoadingAnimationProcess(stopper)
loading.start() # Starts the process
def stop():
stopper.run = False
sleep(1)
# loading.join() ???
return stop # This therefore returns a function to stop the loading, right?
# So this should in theory work:
positions = ["\", "|", "-"]
endfnc = display_loading_until_event("Hello loading")
sleep(4)
endfnc()
# And now the loading SHOULD STOP
此代码只是一个示例,实际代码稍微复杂一些,但应该可以。
加载动画在 powershell 上不起作用(\r
是破坏它的东西。)
当前代码有效,display_loading_until_event
调用后的所有内容都已执行,但加载并未停止。
而且我认为这不是正确的方法...
这种工作:
我只是 return “condition.set” 方法,它起作用了!