在.profile中运行程序会减慢GUI startx的速度

2024-04-29 17:14:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我想运行一个python程序,在启动/登录到我的raspberry pi时轮询键盘。你知道吗

以前的尝试包括cron作业(由于缺少stdin或stdout而失败)。你知道吗

rc.local也失败了,因为它没有stdin(它陷入了一个永久的循环中-现在逃离很有趣)

因此,我将命令放到了.profile中,这似乎很有效!当Pi打开时,程序完全按照预期运行。。。你知道吗

当我试图通过startx启动GUI时,屏幕变黑,完全无法启动。这似乎与Ppython程序有关,因为当我从bash.profile中删除它时,它的所有功能都正常。你知道吗

任何帮助都将不胜感激!你知道吗

更新

我创建了一个脚本,它还输出到LED(一个简单的红黄绿序列),当运行startx时,profile似乎再次执行?如果是,为什么?你知道吗

下面是我的.profile代码,然后是我的python程序

。轮廓线

echo "About to run keyboard polling"; sleep 3
python /home/pi/poll_keyboard.py

投票_键盘.py

import thread
import time
def input_thread(L):
    key = raw_input()
    L.append(key)
    thread.exit() #Should close thread at end
def do_print():
    L = []
    thread.start_new_thread(input_thread, (L,))
    i = 0
    while True:
        print "Hello World %d" % i
        if L: #If anything has been detected
            break
        i += 1
        time.sleep(0.5)
    return L
key = do_print()
print "Key press detected: %s. Exiting in 2" % key
time.sleep(2)
exit()

Tags: keypyimport程序inputtimestdinpi
1条回答
网友
1楼 · 发布于 2024-04-29 17:14:31

首先,您应该只在shell是交互式的情况下运行它

test -t 0 && {
    echo "About to run keyboard polling"; sleep 3
    python /home/pi/poll_keyboard.py
}

其次,Xorg使用Xorg驱动程序(在本例中可能是evdev驱动程序)截取键盘代码。这就是为什么不能用raw_input()记录密钥,如果Xorg也不起作用,我也不会感到惊讶。你知道吗

请记住,您并没有在python代码中记录“键盘”。相反,您正在读取进程stdin(fd=0)。如果这是一个tty,那么你是一种记录键盘,但即使这样,你可能会得到cooked输入无论如何。你知道吗

相关问题 更多 >