1AdityaX / mfrc522-python

The mfrc522-python library is used to interact with RFID readers that use the MFRC522 chip interfaced with a Raspberry Pi.
https://pypi.org/project/mfrc522-python/
GNU General Public License v3.0
13 stars 4 forks source link

Idle time and CPU #1

Closed xijo closed 9 months ago

xijo commented 9 months ago

Awesome lib, thanks a lot!

When running it in daemon mode and waiting for RFID input it is eating a lot of CPU though. Is there a way to throttle, that I did not find just yet?

Cheers, Johannes

xijo commented 9 months ago

For now I worked around with something silly like

while True:
    # Throttle until you find the first block readable
    id, text = reader.read_no_block(11)
    while not id:
        id, text = reader.read_no_block(11)
        sleep(0.5)

    # Continue to read the next block
    if id:
        id2, text2 = reader.read_no_block(15)
        while not id2:
            id2, text2 = reader.read_no_block(15)

    print("full text: ", text + text2)

But I still wonder if there's a smarter solution than that 🤔

1AdityaX commented 9 months ago

Does that code solve your cpu consumption issue?? Try if increasing the sleep work If you could try adding a time.sleep(0.25) below the while loop in MFRC522.py line 307 Screenshot 2024-01-10 025421 lemme know if that works. if that works give me a pull request with the changes

xijo commented 9 months ago

On my PI it looks promising:

With no wait:

     Active: active (running) since Wed 2024-01-10 09:57:38 GMT; 2min 28s ago
        CPU: 2min 27.887s

With 0.1s wait:

     Active: active (running) since Wed 2024-01-10 10:01:11 GMT; 2min 28s ago
        CPU: 1min 9.658s

With 0.5s wait:

     Active: active (running) since Wed 2024-01-10 10:04:12 GMT; 2min 28s ago
        CPU: 25.453s

There was no difference in card detection and reading that I was able to spot.

1AdityaX commented 9 months ago

fixed it!