Python: Do something, then sleep, repeat

I am using a small Python application called Pythonista that allows me to change the color of text on things every few seconds. Here is an example of how I tried to do this in an infinite loop;

while True: v['example'].text_color = 'red' time.sleep(0.5) v['example'].text_color = 'blue' time.sleep(0.5) # and so on.. 

The problem here is that it freezes my program because Python continues to sleep again and again and I never see any changes. Is there a way to see the change (the text changes to red / blue / etc.), and then complete the next task x after a while, etc.?

+7
python sleep
source share
1 answer

You will need to create a new thread that runs your code. Put your code in your own some_function () method, and then start a new thread like this:

 thread = Thread(target = some_function) thread.start() 
+2
source share

All Articles