Поток зависает при отключении сообщения отладки, но работает нормально, когда что-то печатается - PullRequest
0 голосов
/ 30 апреля 2019

Я пытаюсь реализовать программу для одновременного запуска нескольких миди-нот. Каждая нота запускается в отдельном потоке, и есть другой поток, связанный с синхронизацией воспроизводимой фразы.

Я использую MIDO для отправки MIDI-сообщений.

# Threading function for triggering multiple notes at the same time
    def play_thread(self, note, offset):
        thread_handler = handler(self.tempo, self.debug)
        # print("printing solves the problem")
        time.sleep(240* offset /self.tempo)    # Wait until it's time to play
        thread_handler.play_note(note)

    def clock(self, length):
        time.sleep(240 * length / self.tempo)

    # Utility method to play phrase
    def play(self):
        """Utility method to play Phrase. 

        :return: Plays Phrase via MIDIHandler
        :rtype: None 
        """
        # work clock:
        clock = threading.Thread(target=self.clock, args=(self.length,))
        thread_list = []
        for note, start in self.phrase.items():
            this_thread = threading.Thread(target=self.play_thread, args=(note,
                start))
            thread_list.append(this_thread)
        # start all threads
        clock.start()
        for thread in thread_list:
            thread.start()

        # handle endless
        if self.endless:
            clock.join()
            play()
        else:
            for thread in thread_list:
                thread.join()
            clock.join()
            print("Phrase has stopped playing")
...