Noah R
Noah R

Reputation: 5477

What Python Module Should I Use For Updating?

Alright I've been using the time module for time.sleep(x) function for awhile... but I need something that won't pause the shell and so the user can continue using the program while it's counting.

To be more "specific" let's suppose I had a program that needed to wait 5 seconds before executing a function. In this time using the time.sleep() function the user can't type anything into the shell because it's sleeping. However, I need Python to "count the 5 seconds" in the background while the user is able to use the shell. Is this possible?

Upvotes: 0

Views: 93

Answers (1)

Senthil Kumaran
Senthil Kumaran

Reputation: 56861

threading ? You should handle piece of your work in one worker and another separate worker where you would count or sleep with time.sleep

Here is an example that might help you understand and use threading with time.sleep

import threading
import time

def sleeper():
    print 'Starting to sleep'
    time.sleep(10)
    print 'Just waking up..'
    print 'snooze'
    print 'oh no. I have to get up.'

def worker():
    print 'Starting to work'
    time.sleep(1) # this also a work. :)
    print 'Done with Work'

t = threading.Thread(name='sleeper', target=sleeper)
w = threading.Thread(name='worker', target=worker)

w.start()
t.start()

Upvotes: 4

Related Questions