1

I need an alternative to "delay" actions on a LOOP. When using time.sleep(1) the whole process pauses for a second. The print 'something' should be executed after every 1 second, 50 times and not interrupting the rest of the process.

Actual code:

for num in range(50, -1, -1):
    print 'something'
    time.sleep(1)
0

1 Answer 1

1

If I understand what you want, you need to use threads. One thread will do exactly what you did: just count and sleep. The other thread will be "the rest of process", doing whatever it wants, without being interrupted by sleeping.

import time
import threading

def count():
    for num in range(50, -1, -1):
        print 'something'
        time.sleep(1)

count_thread = threading.Thread(target=count)
count_thread.start()

for num in range(10, -1, -1):
    print 'main'
    time.sleep(5)

count_thread.join()
Sign up to request clarification or add additional context in comments.

2 Comments

Any idea with code? I tried threads.Timer, but it keep repeating the action.
Did not solve the problem, if a use a function from a class, the hole class pauses(their process and all). E.g: def count(): for num in range(50, -1, -1): Class().someFunction(num) time.sleep(1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.