Issue
I am writing a little API to make my python command line output look just a little nicer. But there is still a big problem, because I would like to be able to call a function like display_loading_until_event(text)
which should:
- Immediately return a way to "finish" the loading animation
- Update the loading animation every 0.25 seconds.
I also tried using Process
from the multiprocessing
library but it doesn't seem to work:
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
This code is just an example, the real code is a bit more complex but this should do.
The loading animation doesn't work on powershell or so (\r
is the thing that breaks it.)
The current code works and all things after the display_loading_until_event
call get executed but the loading doesn't stop.
And also I don't think that this is the right way to do this...
Solution
This kind of does the job: How to end a while loop in another Thread?
I just return the "condition.set" method and it works!
Answered By - Qrashi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.