I have a while loop in one function listening to incoming messages from a socket connection.

def listenToMidiStream():
    recording = True
    global TRACK_IS_PLAYING

    thread = threading.Thread(target=loopIt)
    thread.start()

    while recording:
        stream = CLIENT_SOCK.recv(1024)
        message = json.loads(stream.decode())

        print(message)

        if message:
            MIDI_OUT.send_message(message)
            if message == [250]:
                TRACK_IS_PLAYING = True
            elif message == [252]:
                TRACK_IS_PLAYING = False

When the function gets called the very first time, it starts a thread where it sends out clock signals. This should stop when the Boolean is set to False.

def loopIt():
    global TRACK_IS_PLAYING

    while TRACK_IS_PLAYING:
        for i in range(0,8):
            MIDI_OUT.send_message(clock)

        time.sleep(interval)

Now the loopIt() gets called once. When a [250] message comes in, I can't recall the function or change the TRACK_IS_PLAYING. I tried several things, but can't really find how I should do this the right way.

有帮助吗?

解决方案

Change your while loop in loopIt() function in this:

while True:
    if TRACK_IS_PLAYING:
        for i in range(0,8):
            MIDI_OUT.send_message(clock)

        time.sleep(interval)

Now, will sending your message only when TRACK_IS_PLAYING is set to True

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top