<div>Hi,</div>
<div>&nbsp;</div>
<div>I am trying to write a cron/ task scheduler like system for my website.&nbsp;The users of my site and also more than one&nbsp;external&nbsp;rss feed reader&nbsp;request my scripts regularly and I&#39;d like to insert code in each script that will&nbsp;decide whether to run a scheduled task or not.
</div>
<div>&nbsp;</div>
<div>It would go something like this:</div>
<div>&nbsp;</div>
<div>&lt;pseudocode&gt;</div>
<div>for task in tasks:</div>
<div>&nbsp;&nbsp;&nbsp; if current_time inside relevant time range for task:</div>
<div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if task not already done:</div>
<div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; record that task has been done</div>
<div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; do task</div>
<div>&lt;/pseudocode&gt;</div>
<div>&nbsp;</div>
<div>Now the problem is that this script could possibly be called more than once simultaneously! Having studied &#39;concurrent programming&#39; I understand the possibility of the following pathological example:</div>
<div>&nbsp;</div>
<div>call 1: if task not already done: </div>
<div>call 2: if task not already done: </div>
<div>call 1:&nbsp;&nbsp;&nbsp;&nbsp; record that task x has been done</div>
<div>
<div>
<div>call 2:&nbsp;&nbsp;&nbsp;&nbsp; record that task x has been done</div>
<div>
<div>call 1: &nbsp;&nbsp;&nbsp; do task x</div>
<div>
<div>call 2:&nbsp;&nbsp;&nbsp; &nbsp;do task x #AGAIN</div>
<div>&nbsp;</div></div>
<div>now we have the task recorded done twice and carried out twice.</div>
<div>&nbsp;</div>
<div>So all my ideas for how to record task x as &#39;done&#39;, like using shelve or&nbsp;writing a log file etc are not sufficient because, I need an &#39;atomic read/ write action&#39; (I think that&#39;s the jargon).</div>

<div>&nbsp;</div>
<div>I thought about asking how to lock a log file so that it couldn&#39;t be accessed simultaneously, but I then realise that you STILL have the problem of the &#39;gap&#39; between &quot;sleep while log file locked&quot; and &quot;access log file&quot;. I need something where the &#39;does the task need doing?&#39; and the &#39;I&#39;m going to do the task. Please no one else do it!&#39;&nbsp;are atomic(?) and leave no room for mischief in between.
</div>
<div>&nbsp;</div>
<div>Another&nbsp;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>&nbsp;</div>
<div>Do let me know if my question isn&#39;t clear.</div>
<div>Thanks in advance!</div>
<div>Tom</div></div></div></div>