本文介绍了Python LIRC阻止信号解决方法无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 Python LIRC 功能 lirc.nextcode()。我关闭了封锁,如果 LIRC 队列为空,则可以跳过代码 lirc.nextcode(),方法是使用 lirc.init(程序,blocking = False),尝试 lirc.set_blocking(False,sockid)。没有工作,代码总是挂起,等待按钮按下,应该继续。

I've been having trouble with the Python LIRC function lirc.nextcode(). I turned off blocking, which allows the code lirc.nextcode() to be skipped if the LIRC queue is empty, by initializing with lirc.init("program", blocking=False) and tried lirc.set_blocking(False, sockid). Neither worked and the code would always hang, waiting for a button press, when it should continue on.

我发现这种解决方法对 raw_input有时间限制( '提示')即可。所以即使我的 lirc.nextcodde()等待按钮按下,如果没有按下按钮来停用闹钟,闹钟将在5秒钟后关闭,并且跳过代码:

I found this workaround that puts a time limit on raw_input('prompt'). So even if my lirc.nextcodde() waits for a button press, an alarm will go off after 5 seconds if no button has been pressed to deactivate the alarm, and skips the code anyway:

import signal

class AlarmException(Exception):
    pass

def alarmHandler(signum, frame):
    raise AlarmException

def nonBlockingRawInput(prompt='', timeout=20):
    signal.signal(signal.SIGALRM, alarmHandler)
    signal.alarm(timeout)
    try:
        text = raw_input(prompt)
        signal.alarm(0)
        return text
    except AlarmException:
        print '\nPrompt timeout. Continuing...'
    signal.signal(signal.SIGALRM, signal.SIG_IGN)
    return ''

然后更改它以适应我的需要:

Then changed it to fit my needs:

import signal
import lirc

sockid = lirc.init('weather', blocking=False)

class AlarmException(Exception):
    pass

def alarmHandler(signum, frame):
    raise AlarmException

def nonBlockingRawInput(prompt='', timeout=5):
    signal.signal(signal.SIGALRM, alarmHandler)
    signal.alarm(timeout)
    try:
        text = lirc.nextcode()
        signal.alarm(0)
        print text
        return text
    except AlarmException:
        print '\nPrompt timeout. Continuing...'
    signal.signal(signal.SIGALRM, signal.SIG_IGN)
    print 'timed out'
    return ''


nonBlockingRawInput()

我想要发生什么: 如果按下按钮并且 LIRC 队列中的IR代码,则应打印按下的按钮。如果没有按下按钮,并且 LIRC 队列为空,则应打印提示超时,持续...和超时。

What I want to happen: If a button has been pressed and an IR code is in the LIRC queue, it should print the button that was pressed. If no button has been pressed and the LIRC queue is empty, it should print "Prompt timeout. Continuing..." and "timed out".

实际发生的情况: 如果按下按钮,并且 LIRC 排队打印按钮,但是如果没有按钮被按下并且队列为空,它将挂起,直到我关闭它。

它的工作原理正确,直到我更改 text = raw_input (提示) text = lirc.nextcode(),那么它挂在该函数上,直到它关闭并发出此错误:

What actually happens: If a button has been pressed and an IR code is in the LIRC queue it prints the button, but if no button has been pressed and the queue is empty it hangs until I close it.
It works exactly as intended until I change text = raw_input(prompt) to text = lirc.nextcode(), then it hangs on that function until it closes and gives this error:

Traceback (most recent call last):
  File "/home/pi/time.py", line 27, in <module>
    nonBlockingRawInput()
  File "/home/pi/time.py", line 16, in nonBlockingRawInput
    text = lirc.nextcode()
  File "/home/pi/time.py", line 10, in alarmHandler
    raise AlarmException
__main__.AlarmException

因此,不仅关闭阻止 lirc.nextcode()的阻止功能不起作用,还可以防止信号警报代码解决方法继续。

So not only does turning off blocking for lirc.nextcode() not work, but it also prevents the Signal alarm code workaround from continuing as well.

这是一个链接 到我的关于 LIRC 阻止的原始问题,这是这种解决方法。我很乐意接受一个答案。

Here is a link "Python Lirc blocks code even when blocking is off" to my original question regarding LIRC blocking, which is what this workaround was for. I'll gladly accept an answer for either.

提前感谢任何帮助,真的很感激。

Thanks ahead of time for any help, it's really appreciated.

推荐答案

切换到 Pylirc2 并使用 pylirc.blocking(0)固定它。

这篇关于Python LIRC阻止信号解决方法无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 22:48