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

Michael Langford mlangford.cs03 at gtalumni.org
Sun Nov 4 22:04:40 CET 2007


You don't need an atomic read and write, you need a blocking file lock
mechanism. This page explains them and how to use them:

http://www.voidspace.org.uk/python/pathutils.html#file-locking

   --Michael

On 11/4/07, Tom <tavspamnofwd at googlemail.com> wrote:
>
> Hi,
>
> 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:
>
> <pseudocode>
> 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
> </pseudocode>
>
> 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!
> Tom
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
Michael Langford
Phone: 404-386-0495
Consulting: http://www.TierOneDesign.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20071104/c701c1fb/attachment-0001.htm 


More information about the Tutor mailing list