ambv / aiotone

A demo of using AsyncIO for music sequencing
MIT License
118 stars 9 forks source link

Question: why locking countdowns? #2

Open ghost opened 3 years ago

ghost commented 3 years ago

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.

ambv commented 3 years ago

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.