<p>I am using RRDTool (<a href="http://oss.oetiker.ch/rrdtool/" rel="nofollow noreferrer">http://oss.oetiker.ch/rrdtool/</a>) as a graphing back-end for storing performance metrics.  This is done via the RRDTool CLI from a Python script.</p>

<p>My problem is that the script is multithreaded and each thread updates the RRD at a pretty rapid pace.  Sometimes an update fails because one thread is accessing the RRD file while another one tries to access it also.</p>

<p>I was under the impression that this is OK to try since RRDTool uses its own locking mechanism, but I guess that isn't true.</p>

<p>Does anyone have a good approach for concurrent access to an RRD?</p>

<p>I can think of a few ways to go:</p>

<ol>
<li><p>have 1 thread create a queue and only feed the RRD from a single thread.</p></li>
<li><p>create my own locking mechanism inside the Python script. (how would I do this?)</p></li>
</ol>

<p>got anything better or have you run into this issue before?</p>

An exclusive lock ought to be enough for this problem :
Python doc page
Use example
Define your lock object at the main level, not at the thread level, and you're done.
Edit in Response to comment :
if you define your lock (
```
lock = new Lock()
```
) at the thread level, you will have one lock object per running thread, and you really want a single lock for the file rrdtool updates, so this definition must be at the main level.