Python Forum
rtmidi problem after running the code x times
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
rtmidi problem after running the code x times
#1
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!
Reply
#2
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  code not running even without errors Azdaghost 2 1,010 Apr-25-2025, 07:35 PM
Last Post: Azdaghost
  python code not running Azdaghost 1 800 Apr-22-2025, 08:44 PM
Last Post: deanhystad
  Problem running external process rjdegraff42 0 787 Mar-19-2025, 07:50 PM
Last Post: rjdegraff42
  writing and running code in vscode without saving it akbarza 5 4,665 Mar-03-2025, 08:14 PM
Last Post: Gribouillis
  problem in running a code akbarza 7 3,299 Feb-14-2024, 02:57 PM
Last Post: snippsat
  the order of running code in a decorator function akbarza 2 1,913 Nov-10-2023, 08:09 AM
Last Post: akbarza
  Different code execution times Wirbelwind94 4 2,335 Oct-06-2023, 12:30 PM
Last Post: snippsat
Question Running an action only between certain times alexbca 9 4,372 Mar-15-2023, 04:21 PM
Last Post: deanhystad
  Code running many times nad not just one? korenron 4 2,774 Jul-24-2022, 08:12 AM
Last Post: korenron
  Error while running code on VSC maiya 4 8,148 Jul-01-2022, 02:51 PM
Last Post: maiya

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.