aiotone icon indicating copy to clipboard operation
aiotone copied to clipboard

Question: why locking countdowns?

Open ghost opened this issue 5 years ago • 1 comments

Hi Łukasz

I followed your awesome PyCon talk. I did a few similar experiments and I just started to write some music with aiotone.

I have a question about this:

https://github.com/ambv/aiotone/blob/e4e999f06d4b649c725085ffc808a059ff4c490c/aiotone/metronome.py#L32-L33

I have used async back in the tornado days and didn't do much recently. I would only lock if the code was:

 async with self.lock: 
     await ...
     .....
     await ...

(Of course we had only yield)

Otherwise there is nothing that can run in between, right? I guess you write it like that in case you change the code to something more complex.

ghost avatar Nov 06 '20 15:11 ghost

You're right that this example it looks artificial without any await expressions inside the lock. You're also right that there might be future changes to code here and a surprise await might indeed change behavior of any of the three methods to become incorrect.

I was interested in expressing that "this should be atomic" in the transactional sense. While it's not strictly necessary, it worked in the context of the talks I was giving for which this code was originally written.

ambv avatar Nov 06 '20 16:11 ambv