Ich verwende Gphoto2, um Bilder auf einer DSLR zu machen. Basiert auf Bash-Befehle, habe ich versucht, subprocess.communicate
zu verwenden, aber es friert ein, nachdem die Kamera ein Foto macht.Python Subprozess kommunizieren friert beim Lesen der Ausgabe
Wenn ich die gphoto2 --capture-image-and-download
im Terminal versuche, dauert es weniger als 2 Sekunden. Ich arbeite an einem Raspberry Pi.
Code:
import subprocess
class Wrapper(object):
def __init__(self, subprocess):
self._subprocess = subprocess
def call(self,cmd):
p = self._subprocess.Popen(cmd, shell=True, stdout=self._subprocess.PIPE, stderr=self._subprocess.PIPE)
out, err = p.communicate()
return p.returncode, out.rstrip(), err.rstrip()
class Gphoto(Wrapper):
def __init__(self, subprocess):
Wrapper.__init__(self,subprocess)
self._CMD = 'gphoto2'
def captureImageAndDownload(self):
code, out, err = self.call(self._CMD + " --capture-image-and-download")
if code != 0:
raise Exception(err)
filename = None
for line in out.split('\n'):
if line.startswith('Saving file as '):
filename = line.split('Saving file as ')[1]
return filename
def main():
camera = Gphoto(subprocess)
filename = camera.captureImageAndDownload()
print(filname)
if __name__ == "__main__":
main()
Wenn ich beende ich diese:
Traceback (most recent call last):
File "test.py", line 39, in <module>
main()
File "test.py", line 35, in main
filename = camera.captureImageAndDownload()
File "test.py", line 22, in captureImageAndDownload
code, out, err = self.call(self._CMD + " --capture-image-and-download")
File "test.py", line 11, in call
out, err = p.communicate()
File "/usr/lib/python2.7/subprocess.py", line 799, in communicate
return self._communicate(input)
File "/usr/lib/python2.7/subprocess.py", line 1409, in _communicate
stdout, stderr = self._communicate_with_poll(input)
File "/usr/lib/python2.7/subprocess.py", line 1463, in _communicate_with_poll
ready = poller.poll()
KeyboardInterrupt
Irgendwelche Ideen?
Zitat „Lesen von Daten von stdout und stderr, bis zum Ende -of-file ist erreicht. " - Wenn der Prozess nicht beendet wurde, bleibt er hängen und wartet auf die Eingabe. Sind Sie sicher, dass Ihr Prozess abgeschlossen ist? – Torxed
Ich bin mir ziemlich sicher. Ich höre den Verschluss der Kamera und eine Datei wird erzeugt. Sobald ich es für mindestens 5 Minuten sitzen lasse aber immer noch einfriere ... Ich habe den Prozess in Terminal getestet und es funktioniert ohne weitere Eingabe. – Dennis
Versuchen Sie etwas ähnliches stattdessen: https://gist.github.com/anonymous/0811f71ca0b290f2625c9ba768bd45fd – Torxed