Monthly Archives: August 2017

Mistake of the day: pyinotify

I’m rewriting an old application. The next-gen evolution here is that instead of a time-based loop, I’m rewiring the flow of execution to take cues via pyinotify. (I was terribly proud of calculating the average song length in my library and using that to decide the loop timing.)

I followed the example code and read the documentation. I found that the key takeaways were

  1. I was to use WatchManager.add_watch to monitor my file. In this case, that’s ~/.quodlibet/current. The documentation clearly states that files and directories both are watchable – though the tutorials only seemed to cover directories.
  2. I watched for the event pyinotify.IN_MODIFY – because I would be waiting for a file modification.

When I tried a dummy run just to see things in action, the event process was triggered as expected. However, processing had fallen through to the “default” event – not IN_MODIFY as I wanted. To make matters worse, no further events were ever seen again (even when they should have happened). I was only getting the one event – and the wrong event, at that.

There’s no head-scratching here. When I added some more verbosity, my gross mistake became very clear. The process of changing ~/.quodlibet/current is to write the new one out to a tempfile and then swap it into the “real” current file. This is two(-ish) events in ~/.quodlibet, and neither of them is a modify to ~/.quodlibet/current.

EDIT: I think the once-off happens because your watch is invalidated. When the tempfile clobbers ~/.quodlibet/current, the underlying file is no more – so you’re watching a ghost whose name was already usurped on the filesystem.

The correct approach is to watch ~/.quodlibet and match on the exact file being reported in the event fire.

J39M

Advertisements

I appreciate blunt friends

I have as of late been moaning and whining incessantly at 2 very blunt friends of mine. The moaning and whining arises from a personal crisis that requires honesty and pragmatism to quell (and possibly address). Some friends I confided in erred on the side of consideration and immediate kindness. I appreciate that – but the wounds within continue festering.

The bluntness is like a sudden splash of cold water, like the shock you get when you drop off the boat thinking that Hawaiian water is warm. In ordinary conversation, it might seem like “well, not really what I was looking for,” but it is a necessary thing that I ultimately look fondly back at.

Bruce rattled the fence as he clambered up. He lithely made his way down, turning right at the corner peach tree, continuing to the grimy bathroom window, and hanging a final left into the homeward stretch.

Bruce stopped. Home was supposed to be here, just off the fence. This was not home.

He paused. He turned in place (an impressive feat even with his feline grace) and retraced his steps – right, past a spotless bathroom window, and left at the apricot tree. Bruce’s nose perked up. He had passed here only moments before, but none of his scent lingered. It was as though he had never been here.

Unnerved, he turned again, one paw slipping audibly on the usually steady fence. Right at the lemon tree, past the blank wall, and left to the home fence. Nothing. A nondescript backyard, not very tidily swept – definitely not Bruce’s home.

Bruce did not turn around again. He continued down the fence. If he was lost, he could always find his way home from the neighborhood streets he knew best. He passed the four-way at the juncture of houses and continued on, eager to hop off the fence.

But the street was not ahead. He came instead to another four-way fence meet-up. Bruce turned left. He must have been accidentally running parallel to the street.

Five fence intersections later, Bruce was still lost and increasingly unsure why all the houses here formed a perfect grid with no egress to the street. Stranger still was the unnatural calm. There were far fewer crickets than August usually provided. Every single house was dark, even though it was scarcely an hour after sundown. Bruce hadn’t run into another living thing – not even a pesky dog to taunt.

Bruce bent and made to drop off the fence.

He froze immediately and couldn’t move. Waves of foreboding crashed over him. The yard, sparsely planted and somewhat unkempt, leered malevolently at him. Bruce knew that he absolutely could not stray from the safety of the fence.

Bruce slowly straightened up.

Meow. A plaintive and confused sound escaped him.

Meow. Bruce just wanted to get home.

Meow. Bruce was scared.

Meow. Bruce was lost.

Meow.

The unending grid of cold houses stretched unimaginably far in all directions.