gTTS Python 脚本在后台获取 tcgetattr():不适合设备的 ioctl

gTTS Python Script in background getting tcgetattr(): Inappropriate ioctl for device

我正在 raspberry Pi 3 上开发一个应用程序,使用 gTTS for Python:

from gtts import gTTS
import os
import threading


def greet_thread(word):
    tts_thread = threading.Thread(target = greet, args=[word])
    tts_thread.start()
def greet(word):
    tts = gTTS(text=word, lang='es')
    tts.save("words.mp3")
    print 'Reproduciendo audio'
    os.system("mpg321 -q presilence.mp3")
    os.system("mpg321 -q words.mp3")

如果我 运行 直接来自 shell 的 python 脚本,这将非常有效。但是如果我在后台执行 python 脚本使用:

python -u script.py > log.txt 2>&1 &

我在日志中收到此错误:

tcgetattr(): Inappropriate ioctl for device

不知道为什么。我认为这是从后台进程调用它的方式,但不知道如何解决它。感谢您的关注和帮助

问题是程序需要使用与执行 GUI 相同的用户来执行。因此,如果您要在命令 shell 中执行它,请避免使用 'root' user.

在我的例子中,我也需要程序在启动时执行。所以我使用 "auto start" 而不是 crontab

解决了它
  1. 导航至 ~/.config/lxsession/LXDE-pi
  2. 纳米自动启动
  3. 编辑文件:

    @lxpanel --profile LXDE-pi

    @pcmanfm --desktop --profile LXDE-pi

    //你的script.sh

    //或@python script.py

    @xscreensaver -无启动画面

    @point-rpi

  4. 保存并退出

  5. 重启