How to use a timer in Python?

Wolfram Kraus kraus at hagen-partner.de
Fri Sep 23 08:31:25 CEST 2005


Nico Grubert wrote:
> Hi there,
> 
> on a Linux machine running Python 2.3.5. I want to create a file 
> 'newfile' in a directory '/tmp' only if there is no file 'transfer.lock' 
> in '/temp'.
> A cronjob creates a file 'transfer.lock' in '/temp' directory every 15 
> minutes while the cronjob is doing something. This job takes around 30 
> seconds. During these 30 seconds the 'transfer.lock' file is present in 
> the '/temp' directory and I must not create 'newfile'. After the cronjob 
> has been finished, the 'transfer.lock' file is deleted from '/temp' and 
> I can create 'newfile'.
> How can I use a timer that waits e.g. 10 seconds if 'transfer.lock' is 
> present and then checks again if 'transfer.lock' is still there?
> 
> I want to do something like this:
> 
> import os
> if 'transfer.lock' in os.listdir('/temp'):
>   # ...wait 10 seconds and then check again if
>   # 'transfer.lock' is in os.listdir('/temp')
> else:
>   # create 'newfile'
> 
> 
> Nico

import time
time.sleep(10)

For more information: help(time.sleep) ;-)

HTH,
Wolfram



More information about the Python-list mailing list