Python PiFace&;asyncio:loop-won';停不下来

Python PiFace&;asyncio:loop-won';停不下来,python,raspberry-pi,Python,Raspberry Pi,我的Raspberry Pi上有一个PiFace板,我想在按下按钮3时退出python程序。因此,我在这个按钮上附加了一个中断侦听器,并在回调中调用loop.stop(),但我的循环不会停止。好像我的命令被忽略了 我的代码: #!/usr/bin/python3 import pifacedigitalio as piface import asyncio import time QUIT_DETECT = 3 loop = asyncio.get_event_loop() counter

我的Raspberry Pi上有一个PiFace板,我想在按下按钮3时退出python程序。因此,我在这个按钮上附加了一个中断侦听器,并在回调中调用loop.stop(),但我的循环不会停止。好像我的命令被忽略了

我的代码:

#!/usr/bin/python3

import pifacedigitalio as piface
import asyncio
import time

QUIT_DETECT = 3

loop = asyncio.get_event_loop()
counter = 0

def onQuit(event):
    # Doesn't stop
    global loop
    loop.stop()
    print(loop)

    # Does change
    global counter
    counter += 1
    print(counter)

piface.init()

listener = piface.InputEventListener()
listener.register(QUIT_DETECT, piface.IODIR_FALLING_EDGE, onQuit)
listener.activate()

loop.run_forever()

loop.close()
listener.deactivate()
piface.deinit()
输出为:

<_UnixSelectorEventLoop running=True closed=False debug=False>
1
<_UnixSelectorEventLoop running=True closed=False debug=False>
2
<_UnixSelectorEventLoop running=True closed=False debug=False>
3
<_UnixSelectorEventLoop running=True closed=False debug=False>
4

1.
2.
3.
4.

因此,
计数器
在每次按下时都会增加,但
循环
不会接受我的停止呼叫。谁能告诉我如何停止循环?提前感谢

我想您已经运行了一些协同程序并做了一些工作,因为在这段代码中不需要异步IO,它只是被错误地用于阻塞

尽管如此,我很确定您有一个线程问题

查看模块中的gitHub:

piface.InputEventListener()
来自pifacecommon.interrupts.PortEventListener的数据

class InputEventListener(pifacecommon.interrupts.PortEventListener):

PortEventListener使用线程和多处理

import threading
import multiprocessing
[....]
class PortEventListener(object):
"""Listens for port events and calls the registered functions.
>>> def print_flag(event):
...     print(event.interrupt_flag)
...
>>> port = pifacecommon.mcp23s17.GPIOA
>>> listener = pifacecommon.interrupts.PortEventListener(port)
>>> listener.register(0, pifacecommon.interrupts.IODIR_ON, print_flag)
>>> listener.activate()
"""

TERMINATE_SIGNAL = "astalavista"

def __init__(self, port, chip, return_after_kbdint=True, daemon=False):
    self.port = port
    self.chip = chip
    self.pin_function_maps = list()
    self.event_queue = EventQueue(self.pin_function_maps)
    self.detector = multiprocessing.Process(
        target=watch_port_events,
        args=(
            self.port,
            self.chip,
            self.pin_function_maps,
            self.event_queue,
            return_after_kbdint))
    self.detector.daemon = daemon
    self.dispatcher = threading.Thread(
        target=handle_events,
        args=(
            self.pin_function_maps,
            self.event_queue,
            _event_matches_pin_function_map,
            PortEventListener.TERMINATE_SIGNAL))
    self.dispatcher.daemon = daemon

似乎在另一个线程中运行,然后给你回电话。显然,停止不会到达主线程中的asyncio.loop


同意吗,到目前为止

理想情况下,您应该在一个线程中使用线程或异步编程

尽管如此,我还是希望它能够有效地移交循环参考,而不是使用它作为全局参考: def onQuit(循环):

或 从functools导入部分 寄存器(退出检测,piface.IODIR\u下降沿,部分(onQuit,循环))


您是否尝试在onQuit中重新获取循环而不是全局使用? asyncio.get\u事件\u循环()

玩得开心
Danny

您确定asyncio应该在raspberry pi上运行吗?(也许这不是覆盆子派,原因是:D)@vlad ardelean我想是的。为什么不呢?同时,我将尝试使用
屏障
对象的解决方法。
listener.register(QUIT_DETECT, piface.IODIR_FALLING_EDGE, onQuit(loop))