rtmidi problem after running the code x times - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: rtmidi problem after running the code x times (/thread-33163.html) |
rtmidi problem after running the code x times - philipbergwerf - Apr-03-2021 def midi_input(): class Collector(threading.Thread): def __init__(self, device, port): threading.Thread.__init__(self) self.setDaemon(True) self.port = port self.portName = device.getPortName(port) self.device = device self.quit = False def run(self): self.device.openPort(self.port) self.device.ignoreTypes(True, False, True) while True: if self.quit: return msg = self.device.getMessage(2500) if msg: if msg.isNoteOn(): if midi_record_toggle == 1: note = msg.getNoteNumber() - 20 textw.insert(textw.index(INSERT), number2pitch[note]) render('q', papercolor) dev = rtmidi.RtMidiIn() for i in range(dev.getPortCount()): device = rtmidi.RtMidiIn() print('OPENING',dev.getPortName(i)) collector = Collector(device, i) collector.start() sys.stdin.read(1) for c in collectors: c.quit = True threading.Thread(target=midi_input).start()I am running this using threading. I don't know what the risks are... The code runs fine but if I run it more than 16 times it gives an Error: 'ALSA lib seq_hw.c:466:(snd_seq_hw_open) open /dev/snd/seq failed: Cannot allocate memory. I think it doesn't close the midi port/process but I don't know how to close this correctly. I noticed that Sunvox(a really cool synth) gives the same error on midi input after I messed up with my code. Is there anything suspicious about my code? how am I supposed to use threading; I used this to make a while loop run while other functions are still working. I took the code from a GitHub page and modified it a little. Thanks! RE: rtmidi problem after running the code x times - philipbergwerf - Apr-04-2021 I found the solution to this problem. I made a variable 'midiswitch' that is standard set to 1 and when the program closes it is set to 0. The while loop checks if the variable is 0 and closes the loop if so. def midi_input(): class Collector(threading.Thread): def __init__(self, device, port): threading.Thread.__init__(self) self.setDaemon(True) self.port = port self.portName = device.getPortName(port) self.device = device self.quit = False def run(self): self.device.openPort(self.port) self.device.ignoreTypes(True, False, True) while True: if midiswitch == 0: return msg = self.device.getMessage(2500) if msg: if msg.isNoteOn(): if midi_record_toggle == 1: note = msg.getNoteNumber() - 20 if shiftkey == 0: textw.insert(textw.index(INSERT), number2pitch[note]) elif shiftkey == 1: textw.insert(textw.index(INSERT), '_'+number2pitch[note]) renderkey() dev = rtmidi.RtMidiIn() for i in range(dev.getPortCount()): device = rtmidi.RtMidiIn() print('OPENING',dev.getPortName(i)) collector = Collector(device, i) collector.start() threading.Thread(target=midi_input).start() |