【问题标题】:Raspberry Pi: terminate raspistill command from pythonRaspberry Pi:从 python 终止 raspistill 命令
【发布时间】:2013-09-16 10:33:59
【问题描述】:

我正在尝试使用服务器上的 PHP 文件将一些变量传输到 Python 脚本中,这将反过来在我的 Raspberry Pi 上启动 raspistill 延时摄影。

到目前为止,我已经设法开始拍照,但我现在想要一个按钮来终止游戏中时光倒流 - 我尝试了很多方法,包括 .kill() 和 .terminate() 但无法让它工作。

这是我当前的 python 代码:

import sys, os, time, datetime
import subprocess
import signal
from time import sleep

tlfreq = int(sys.argv[1])
tltime = int(sys.argv[2])
dir = '/var/www/timelapse/' + sys.argv[3]

if not os.path.exists(dir):
    os.makedirs(dir)

cmd = ('raspistill -t ' + str(tltime) + " -tl " + str(tlfreq) + " -o " + dir + "/photo_%04d.jpg")
pro = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                   shell=True, preexec_fn=os.setsid)
print "Pictures are now being taken every" , tlfreq/1000 , "second/s for a total of", tltime/3600000 , "hours. These are being stored in", dir

也许我需要一个“if variable = 1 then kill”命令,然后将变量发送到 python。

任何帮助将不胜感激!

非常感谢, 丹

【问题讨论】:

  • 您可以调用os.killpg(pro.pid, signal.SIGTERM) 来终止pro 子进程及其所有子进程。不要使用stdout=subprocess.PIPE,除非您稍后阅读pro.stdout

标签: python linux subprocess raspberry-pi


【解决方案1】:

您可以使用此代码创建新的 python 脚本 kill_raspystill.py

import os

os.system("pkill raspistill")

并在您按下按钮时调用该脚本。

【讨论】:

  • 注意:它试图杀死所有raspistill 命令。你可以使用subprocess.check_call(['pkill', 'raspistill']) 来避免产生一个shell。
【解决方案2】:

【讨论】:

    猜你喜欢
    • 2016-12-23
    • 2012-11-14
    • 2021-07-11
    • 2018-11-16
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多