Unless you use the nowait value (with 8), flock blocks until it's flocked, or until it would never have flocked (file not found, or no permissions to do such, for example).
No loop needed here.
But you also don't want to flock after opening for writing like that. You've just clobbered the file even if someone is using it!