<div>Hi,</div>
<div> </div>
<div>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.
</div>
<div> </div>
<div>It would go something like this:</div>
<div> </div>
<div><pseudocode></div>
<div>for task in tasks:</div>
<div> if current_time inside relevant time range for task:</div>
<div> if task not already done:</div>
<div> record that task has been done</div>
<div> do task</div>
<div></pseudocode></div>
<div> </div>
<div>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:</div>
<div> </div>
<div>call 1: if task not already done: </div>
<div>call 2: if task not already done: </div>
<div>call 1: record that task x has been done</div>
<div>
<div>
<div>call 2: record that task x has been done</div>
<div>
<div>call 1: do task x</div>
<div>
<div>call 2: do task x #AGAIN</div>
<div> </div></div>
<div>now we have the task recorded done twice and carried out twice.</div>
<div> </div>
<div>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).</div>
<div> </div>
<div>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.
</div>
<div> </div>
<div>Another problem is that the task may fail and need redoing, but I think I can solve that given a solution to the above.</div>
<div> </div>
<div>Do let me know if my question isn't clear.</div>
<div>Thanks in advance!</div>
<div>Tom</div></div></div></div>