fsmeraldi / bleakheart

An asynchronous BLE Heart Monitor library with support for additional data from Polar monitors (ECG, accelerometers, etc)
Mozilla Public License 2.0
10 stars 9 forks source link

AsyncIO issue prevents programs from running #3

Closed HufflyCodes closed 4 weeks ago

HufflyCodes commented 5 months ago

Been a bit since I have updated anything here as I was going through testing BleakHeart on Windows 10 but one issue I did run into right off the bat was the keyboard handler AsnycIO reader on line 67 in the ECG Queue example prevents the program from starting up. While that's a nice feature for sure but my current work around is just commenting out loop.add_reader(sys.stdin, keyboard_handler) I haven't had the time to fully check why this is an issue on Windows 10 as I am fairly new to AsyncIO so when I get around to doing a deeper dive I will look into it further.

MarkSttc commented 4 months ago

Hi @HufflyCodes, I'm not the expert, but it seems like Windows does not support "keyboard_handler" in asyncio.

If anyone knows how to get around with this, please let us know.

fsmeraldi commented 4 months ago

Hi @HufflyCodes, I am wondering if the msvcrt module would be any help, see this discussion.

Otherwise, a possible workaround would be to remove the add_reader line (and the handler function) as you have done, and instead get the consumer task to time out and set the quitclient event when no data is added to the queue for a certain time. For that, you'd need to replace the lines

frame = await queue.get()
if frame[0]=='QUIT':   # intercept exit signal
            break

with something like this:

try:
   # raises TimeoutError if the queue is still empty after 10 seconds
    frame = await asyncio.wait_for(queue.get(), timeout=10)
except asyncio.TimeoutError:
     quitclient.set()
     break

and define the quitclient event globally, or pass it to both tasks. At this point, the last three lines of run_ble_client are no longer needed:

        ### remove these lines
        loop.remove_reader(sys.stdin)
        # signal the consumer task to quit
        queue.put_nowait(('QUIT', None, None, None))

I have not tried it, but barring mistakes I think it should work. The program should quit by itself when the user takes off the sensor (or the sensor disconnects)

Hope this helps! Fabrizio

alex-ong commented 4 weeks ago

Thanks for raising this issue, i was scratching my head, and commenting out the line enabled my Coospo 808s to work on the heartrate_callback example

fsmeraldi commented 4 weeks ago

Thanks @alex-ong for testing it on the Coospo device!

@HufflyCodes and @MarkSttc , I have now modified the examples so that they use a separate thread for keyboard input under windows.

I'll close this issue, as I think this is now solved Fabrizio