Gathering and logging data from a plugin: how to do it without race conditions?

You can create a lock by using flock on one of your existing files (or create a special one fo this. Get the lock before you start the canculation and release it after the result was stored.