0
class Session(list) :

        def __init__(self, session_file, laps_sound_file, end_sound_file, autoparse=True) :

            self.session_file = session_file
            self.laps_sound_file = laps_sound_file
            self.end_sound_file = end_sound_file

            self.cont_flag = threading.Event()
            self.cont_flag.set()

            if autoparse : self.parse()

        def start(self) :

            print("Entrainement Are going to start in : 10 sec\n" * 10)
            time.sleep(10)

            for action in self :

                if not self.cont_flag.is_set() :
                    print('pause\n' * 10)
                    self.cont_flag.wait()

                os.system('cls')

                print(str(action) * 10)
                winsound.PlaySound(self.laps_sound_file.split('.')[0], winsound.SND_FILENAME)
                time.sleep(action.time)

            winsound.PlaySound(self.end_sound_file.split('.')[0], winsound.SND_FILENAME)

        def pause(self) :
            self.cont_flag.clear()

        def cont_session(self) :
            self.cont_flag.set()

i have 2 thread in my programm, the first thread is waiting console input and the second thread is executing start methode of class Session

the first thread is acting like a control panel on the session thread, there is the code :

while 1 :
    command = input("enter you're command : ").strip().lower()

if command == 'exit' :
    break
elif command == 'pause' :
    sess.pause()
elif command == 'continue' :
    sess.cont_session()
else : print('invalide command, please retry')

so i want to know if i must use a lock between :

if not self.cont_flag.is_set() :
            print('pause\n' * 10)
            self.cont_flag.wait()

and

def pause(self) :
        self.cont_flag.clear()
3
  • Subclassing list is never helpful. It often leads to unexpected, undefined behavior. Commented Feb 13, 2014 at 20:59
  • its better then to implement actions in a list inside the Session class? Commented Feb 13, 2014 at 21:01
  • Yes, that would be typical. Commented Feb 13, 2014 at 21:12

1 Answer 1

1

No, you don't need a lock here. The whole point of an Event is that it's a synchronized flag.


That being said, your code has a race, and a lock won't help it—but it might be what you want anyway.

In this code:

if not self.cont_flag.is_set() :
    print('pause\n' * 10)
    self.cont_flag.wait()

… it is perfectly possible for the flag to be set and then cleared again between the is_set and the wait. In that case, you'll end up waiting until the flag is once again set.

But, unless you want to guarantee that the loop runs at least once for every continue (no matter how quickly you re-pause), or that "Pause" always gets printed out for each pause (no matter how quickly you re-continue and re-pause), that may be what you wanted to happen.


If you want to eliminate that race, you probably want a Condition and a regular bool variable, not an Event. While events are very simple, they're also very hard to compose into anything less simple…

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.