Unable to emit from a GPIO.add_event_detect
I'm trying to send some data to a client when an LED blinks thats connected to an IO on a raspberry pi.
I know this is a threading issue I'm just not sure how to handle it. the emit needs to be awaited which means the callback function that the GPIO.add_event_detect calls needs to be async, but then that callback function needs to be awaited when called from the GPIO event. I'm not sure how to handle this. The below produces "RuntimeWarning: coroutine 'my_callback_one' was never awiated"
sio = socketio.AsyncServer(async_mode='sanic')
app = Sanic()
sio.attach(app)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
async def my_callback_one(channel):
await sio.emit('my_response', {'data': 'test'})
print('========button press LED lighting detected========')
GPIO.add_event_detect(18, GPIO.RISING, callback=my_callback_one)
Is the GPIO library compatible with asyncio? You can't use code that was written to be synchronous with asyncio, unfortunately.
Thanks for getting back to me, and I unfortunately have no idea if it is or not. Does socketio utilize asyncio?
If it was supported would the above code be expected to work?
Is there a better method that I could use to get threaded call backs from up interrupts up to my web app?
This Python socket.io llbrary works with or without asyncio. You made a choice of using the asyncio support when you decided to use Sanic as a web server.
If GPIO was asyncio compatible then I'd expect it would work with your code.
The solution is to use a GPIO library that is compatible with asyncio. Either that, or switch the script entirely out of asyncio.
I see, I'll have to see how I can do this with a library that supports asyncio or otherwise somehow wrap the gpio library threaded interrupt event handler stuff in asyncio somehow(?)
I just found this, who's answer seems like it might be in the same realm of what I'm trying to do? It kind of looks like he's wrapped the threaded call back in an asyncio event loop. I'm not sure if that would work for calling an emit from though.
https://raspberrypi.stackexchange.com/questions/54514/implement-a-gpio-function-with-a-callback-calling-a-asyncio-method
Also, would ditching sanic and just using flask solve my problems with asyncio?
Edit: Thanks again for giving me some guidance, I've got 2 weeks to get this thing working, its killing me.
This is a tricky problem. Your server is asynchronous, so you cannot have any blocking libraries. Even if you switch to Flask, you will probably be using eventlet or gevent, which are sort of similar to asyncio and have the same constraints with regards to blocking code.
Basically you need to find a GPIO library that is asynchronous. If you find one for asyncio, then you can keep using Sanic. If you find one for a greenlet framework, then you will be better off ditching asyncio and going with Flask plus either eventlet or gevent.
I see, the GPIO.add_event_detect function itself doesn't block as it fires off a thread in C, when that thread comes back (an interrupt is hit, IE the led lit up) it runs the function specified (my_callback_one), but I would need a way to await that function call it seems as the emit function inside is required to be awaited (as its asyncio) but I don't see how I would do that with gpio as you said.
@Souldat I doubt this is something that you can fix on your side. Libraries either support asyncio or they don't, it's really really hard to have Python code that is compatible with both approaches.
I think the best / easiest solution for me right now is to just write my gpio results to a file and read that file from my other thread on a loop and send that up to the client.