473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to use a timer in Python?

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
Sep 23 '05 #1
3 1608
Nico Grubert enlightened us with:
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?


Make a while loop in which you sleep. Or use the threading module if
you want to go multi-threading.

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Sep 23 '05 #2
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
Sep 23 '05 #3
Nico Grubert <ni*********@gmail.com> wrote:
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'.


That all sounds very race-y to me! The cron-job and the other process
need to take the same lock, otherwise the cron-job will start 1ms
after the other process checks for transfer.lock and before it has a
chance to create newfile and there will be trouble.

Using files as locks isn't brilliant because the operations "read to
see if the lock is there" and "create the file isn't" aren't atomic.
Ie someone can get in there after you read the directory but before
you create the file.

However creating a directory is atomic, so you can take the lock by
os.mkdir("/tmp/lock"). If that succeeded you got the lock, if it
failed (threw OSError) then you didn't. If it failed then just
time.sleep(1) and try again. This kind of locking works cross
platform too. You can use it in shell too, eg "mkdir /tmp/lock ||
exit 1" in your cronjob.

You could wrap the locking up into a module of course, and I bet
someone already did.

--
Nick Craig-Wood <ni**@craig-wood.com> -- http://www.craig-wood.com/nick
Sep 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: Derek Fountain | last post by:
Does Python have a timer mechanism? i.e. an "after 500 milliseconds, run this bit of code" sort of thing?
8
by: bearophileHUGS | last post by:
Hello, I have four things to ask or to suggest, sorry if they seem basic or already discussed. ------------------- I am still ignorant about Tkinter. This little program, after pressing the...
2
by: yxh | last post by:
how to use timer in python. the functionnality is like the MFC SetTimer()
1
by: chewjeremy | last post by:
I'm planning to do C++ development together with Python on Linux. I understand that Linux allows only one timer per process. Does Python use the Linux timer? If so, how do I use the timer without...
1
by: Dongsheng Ruan | last post by:
Hi Does anybody know how to pass multiple arguments to the function tested in timeit.timer() in python? I googled and found how to pass one argument: x=10000 mytime = timeit.Timer(...
3
by: silverburgh.meryl | last post by:
Hi, I have a function in my python like this: def callFunc(line, no): # some code And I want to do a performance test like this: for line in f: for i in range(int(count)): t1 =...
1
by: Gabriel Genellina | last post by:
En Fri, 30 May 2008 22:50:13 -0300, Robert Dailey <rcdailey@gmail.com> escribió: Use an Event object; its wait() will provide the sleep time, and when it is set() the thread knows it has to...
4
by: John Dann | last post by:
I need what I'd call (in .Net) a timer, ie I need to run a function eg every 2 seconds - it doesn't need to be millisec accurate but it would be nice if it wasn't eg every 4 seconds or something. ...
3
by: 5lvqbwl02 | last post by:
Windows XP SP3 Python 2.5 wx.version() = '2.8.1.1 (msw-unicode)' ------ I have written the following *simplest* implementation of wx.timer I can think of. No workie. I want an exception, a...
7
by: ssecorp | last post by:
I am not clear about the results here. from timeit import Timer import Decorators def fib(n): a, b = 1, 0 while n: a, b, n = b, a+b, n-1
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.