Due to my question here, I have to monitor a file-descriptor in the background.
I have tried this:
import gpiod
import asyncio
import select
async def read():
name = "GPOUT"
line = chip.get_line(INPUT)
line.request(consumer=name, type=gpiod.LINE_REQ_EV_BOTH_EDGES, flags=gpiod.LINE_REQ_FLAG_BIAS_PULL_UP)
fd = line.event_get_fd()
poll = select.poll()
poll.register(fd)
loop = asyncio.get_event_loop()
while True:
await loop.run_in_executor(None, poll.poll, None)
event = line.event_read()
print(event)
def main():
asyncio.run(read())
while True:
input = input("Enter something: ")
print(input)
main()
The execution is blocked at call off poll.poll
. I have also tried this:
import gpiod
import asyncio
def readCb(line):
print(f"{line.consumer}: {line.event_read()}")
def read():
name = "GPOUT"
line = chip.get_line(INPUT)
line.request(consumer=name, type=gpiod.LINE_REQ_EV_BOTH_EDGES, flags=gpiod.LINE_REQ_FLAG_BIAS_PULL_UP)
fd = line.event_get_fd()
loop = asyncio.get_event_loop()
loop.add_reader(fd, readCb, line)
loop.run_forever()
def main():
read()
while True:
input = input("Enter something: ")
print(input)
main()
Also blocks in the read function.
For the second example, I also tried to make read()
async
and call it with asyncio.run()
but then the following error occurs:
File "/usr/lib/python3.9/asyncio/base_events.py", line 578, in _check_running
raise RuntimeError('This event loop is already running')
Is there any possibility to do this in python, besides real threads and subprocesses?
Many thanks for your help and best regards,
Cone