You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hey there, I wanted to use this framework in a larger application, where some actions trigger a read or write, which are in a side thread of the program. The issue is that the read or write locks the thread and can not be canceled because the read or write action may not be executed. When doing a read or write action again after not finishing the old one, it's no longer possible to read / write. A message "Segmentation Error" is shown when exiting the program.
Basic example code can be found below, full code can be found here. In summary, a thread is spun up which should run a loop of reading or writing. The cancel function breaks the loop. This does not work tough, because .read() and .write() will lock until the read or write did happen. Even if the component got cleaned up by the garbage collector, I assume some lower level read/write code is still running, and if using one of the both functions again will cause the described error. If I always read or write a card/chip after using the function (which is not always the case because it's optional) the program runs as intended.
Is there any option or possibility to cancel the read progress over the SimpleMFRC522, so I can implement an optional read into my program?
frommfrc522importSimpleMFRC522classBasicMFRC522():
def__init__(self) ->None:
self.rfid=SimpleMFRC522()
defread_card(self) ->Union[str, None]:
_, text=self.rfid.read()
iftextisnotNone:
text=text.strip()
returntextdefwrite_card(self, text: str) ->bool:
_id, _=self.rfid.write(text)
return_idisnotNoneclassRFIDReader:
def__init__(self) ->None:
self.is_active=Falseself.rfid=BasicMFRC522()
defread_rfid(self, side_effect: Callable[[str], None]):
"""Start the rfid reader, calls an side effect with the read value"""ifself.is_active:
returnself.is_active=Truerfid_thread=Thread(target=self._read_thread, args=(side_effect,), daemon=True)
rfid_thread.start()
def_read_thread(self, side_effect: Callable[[str], None]):
"""Execute the reading until reads a value or got canceled"""text=Nonewhileself.is_active:
text=self.rfid.read_card()
iftextisnotNone:
side_effect(text)
breaktime.sleep(0.5)
self.is_active=Falsedefwrite_rfid(self, value: str, side_effect: Optional[Callable[[str], None]] =None):
"""Writes the value to the RFID"""ifself.is_active:
returnself.is_active=Truerfid_thread=Thread(target=self._write_thread, args=(value, side_effect,), daemon=True)
rfid_thread.start()
def_write_thread(self, text: str, side_effect: Optional[Callable[[str], None]] =None):
"""Executes the writing until successful or canceled"""whileself.is_active:
success=self.rfid.write_card(text)
ifsuccess:
ifside_effect:
side_effect(text)
breaktime.sleep(0.1)
self.is_active=Falsedefcancel_reading(self):
"""Cancels the reading loop"""self.is_active=False
Thanks for the time.
Update: After some more diving into the source code and talking to a friend, I think read_no_block and write_no_block instead of the read and write function should do the general job? Is there anything to look out for or do in addition when interrupting the read/write?
The text was updated successfully, but these errors were encountered:
Hey there, I wanted to use this framework in a larger application, where some actions trigger a read or write, which are in a side thread of the program. The issue is that the read or write locks the thread and can not be canceled because the read or write action may not be executed. When doing a read or write action again after not finishing the old one, it's no longer possible to read / write. A message "Segmentation Error" is shown when exiting the program.
Basic example code can be found below, full code can be found here. In summary, a thread is spun up which should run a loop of reading or writing. The cancel function breaks the loop. This does not work tough, because
.read()
and.write()
will lock until the read or write did happen. Even if the component got cleaned up by the garbage collector, I assume some lower level read/write code is still running, and if using one of the both functions again will cause the described error. If I always read or write a card/chip after using the function (which is not always the case because it's optional) the program runs as intended.Is there any option or possibility to cancel the read progress over the SimpleMFRC522, so I can implement an optional read into my program?
Thanks for the time.
Update: After some more diving into the source code and talking to a friend, I think
read_no_block
andwrite_no_block
instead of theread
andwrite
function should do the general job? Is there anything to look out for or do in addition when interrupting the read/write?The text was updated successfully, but these errors were encountered: