Issue
I cobbled together the following code from an example in https://websockets.readthedocs.io/en/stable/intro.html#python-lt-36 This would no doubt be simpler in Python 3.6+, but unfortunately the distro I am using (Raspbian) hosts Python 3.5.3. Clearly, I have done something wrong, but I don't know what. What did I do wrong, and is there a simpler approach?
def my_callback1(channel): # Down Arrow Pressed
Return, Cursor = update.upArrow()
if Return:
message = "Control, " + str(Cursor)
asyncio.get_event_loop().run_until_complete(SendMessage(message))
@asyncio.coroutine
def SendMessage(message):
websocket = yield from websockets.connect('ws://localhost:8000')
try:
yield from websocket.send(message)
finally:
yield from websocket.close()
GPIO.add_event_detect(pinsarrow[0], GPIO.RISING, callback=my_callback1, bouncetime=bTime) # Down Arrow
Traceback (most recent call last):
File "/usr/local/sbin/button.py", line 25, in my_callback1
asyncio.get_event_loop().run_until_complete(SendMessage(message))
File "/usr/lib/python3.5/asyncio/events.py", line 671, in get_event_loop
return get_event_loop_policy().get_event_loop()
File "/usr/lib/python3.5/asyncio/events.py", line 583, in get_event_loop
% threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'Dummy-1'.
Solution
Here is the full solution, thanks to shmee:
def my_callback1(channel): # Down Arrow Pressed
Return, Cursor = update.downArrow()
if Return:
message = "Control, " + str(Cursor)
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(SendMessage(message))
@asyncio.coroutine
def SendMessage(message):
websocket = yield from websockets.connect('ws://localhost:8000')
try:
yield from websocket.send(message)
finally:
yield from websocket.close()
GPIO.add_event_detect(pinsarrow[0], GPIO.RISING, callback=my_callback1, bouncetime=bTime) # Down Arrow
Answered By - lrhorer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.