简体   繁体   English

python loop():raw_input()EOF错误:读取一行时出现EOF

[英]python loop(): raw_input() EOFError: EOF when reading a line

Here is script (/shutdown.py). 这是脚本(/shutdown.py)。 It monitors button press and if button is pressed more than 3 seconds, it runs poweroff command. 它监视按钮的按下,如果按下按钮的时间超过3秒钟,它将运行poweroff命令。

#!/usr/bin/python

# Import the modules to send commands to the system and access GPIO pins
from subprocess import call
from time import sleep
import RPi.GPIO as gpio
import time

# Define a function to keep script running
def loop():
    raw_input()

# Define a function to run when an interrupt is called
def shutdown(pin):
    button_press_timer = 0
    while True:
        if (gpio.input(17) == False) : # while button is still pressed down
            button_press_timer += 1 # keep counting until button is released
            if button_press_timer == 3:
                #print "powering off"
                call('poweroff', shell=True)
            sleep(1)
        else: # button is released, figure out for how long
            #print "Poga atlaista. nospiesta bija " + str(button_press_timer) + " sekundes"
            #button_press_timer = 0
            return
#       sleep(1) # 1 sec delay so we can count seconds
#    print "powering off"

gpio.setmode(gpio.BCM) # Use BCM GPIO numbers
gpio.setup(17, gpio.IN, pull_up_down=gpio.PUD_UP) # Set up GPIO 17 as an input
gpio.add_event_detect(17, gpio.FALLING, callback=shutdown, bouncetime=200) # Set up an interrupt to look for button presses

loop() # Run the loop function to keep script running

If I run script from console like /shutdown.py all is fine. 如果我从/shutdown.py控制台运行脚本,一切都很好。 Button press is detected and system shutdown is initialed. 检测到按下按钮,并启动系统关闭程序。 But if i add that script to /etc/rc.local (/shutdown.py &), then it fails at startup with this error: 但是,如果我将该脚本添加到/etc/rc.local ),则启动时会失败,并显示以下错误:

Traceback (most recent call last):
  File "/shutdown.py", line 35, in <module>
    loop() # Run the loop function to keep script running
  File "/shutdown.py", line 11, in loop
    raw_input()
EOFError: EOF when reading a line

If I comment out loop() line, then there is no error and script does not run in background. 如果我注释掉loop()行,则没有错误,并且脚本不在后台运行。 I just start and exit and button press not detected. 我只是启动和退出,未检测到按钮按下。 So, how i can run that script at startup and keep running in background? 那么,我如何在启动时运行该脚本并保持在后台运行?

EDIT 编辑

I am not python guru and i think that loop() is python internal function. 我不是python专家,我认为loop()是python内部函数。 Now i seen that it is defined function which calls raw_input() . 现在我看到它是定义的函数,它调用raw_input() That script I found and modified to fit my needs. 我找到并修改了该脚本以适合我的需求。 Thanks. 谢谢。

What you really need is a Python daemon which runs in the background. 您真正需要的是在后台运行的Python守护程序。 The raw_input method you are trying to use looks like an ugly hack to me. 您尝试使用的raw_input方法对我来说似乎是一个丑陋的hack。

Have a look at python-daemon package, which is meant exactly for your use case and is quite simple to use. 看一下python-daemon软件包,它完全适合您的用例,并且使用起来非常简单。 There is also an updated fork with Python 3 support. 还有一个更新的fork支持Python 3。

After installing python-daemon, add this line to the beginning of your script 安装python-daemon之后,将此行添加到脚本的开头

import daemon

Then substitute the loop() call at the end of your script with this code: 然后用以下代码替换脚本末尾的loop()调用:

with daemon.DaemonContext():
    while True:
        time.sleep(10)

This code is untested, but you get the idea. 这段代码未经测试,但是您可以理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM