2011-01-13 52 views
0

好吧我一直在使用time.sleep(x)函数的时间模块一段时间......但我需要的东西不会暂停shell,因此用户可以在计数时继续使用该程序。我应该使用什么Python模块进行更新?

为了更“具体”,让我们假设我有一个程序需要等待5秒钟才能执行一个函数。在这个时候使用time.sleep()函数,用户不能在shell中输入任何东西,因为它正在睡觉。但是,我需要Python在后台“计数5秒”,同时用户可以使用shell。这可能吗?

+1

你的问题很不明确,但听起来像你需要一个线程。 – Falmarri 2011-01-13 16:37:22

+1

这可能与您的兴趣有关:http://stackoverflow.com/questions/3226628/non-blocking-wait-in-python – birryree 2011-01-13 16:39:08

回答

4

threading?你应该处理一件工作的一名工人,另一个单独的工作,你会数或time.sleep

睡在这里是可以帮助你理解和使用与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() 
相关问题