[Tutor] I need an 'atomic read/ write action'

Tom tavspamnofwd at googlemail.com
Sun Nov 4 16:55:05 CET 2007


I am trying to write a cron/ task scheduler like system for my website. The
users of my site and also more than one external rss feed reader request my
scripts regularly and I'd like to insert code in each script that
will decide whether to run a scheduled task or not.

It would go something like this:

for task in tasks:
    if current_time inside relevant time range for task:
        if task not already done:
            record that task has been done
            do task

Now the problem is that this script could possibly be called more than once
simultaneously! Having studied 'concurrent programming' I understand the
possibility of the following pathological example:

call 1: if task not already done:
call 2: if task not already done:
call 1:     record that task x has been done
 call 2:     record that task x has been done
 call 1:     do task x
 call 2:     do task x #AGAIN

now we have the task recorded done twice and carried out twice.

So all my ideas for how to record task x as 'done', like using shelve
or writing a log file etc are not sufficient because, I need an 'atomic
read/ write action' (I think that's the jargon).

I thought about asking how to lock a log file so that it couldn't be
accessed simultaneously, but I then realise that you STILL have the problem
of the 'gap' between "sleep while log file locked" and "access log file". I
need something where the 'does the task need doing?' and the 'I'm going to
do the task. Please no one else do it!' are atomic(?) and leave no room for
mischief in between.

Another problem is that the task may fail and need redoing, but I think I
can solve that given a solution to the above.

Do let me know if my question isn't clear.
Thanks in advance!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20071104/be6e93ab/attachment.htm 

More information about the Tutor mailing list