ffoiled
Posts: 52
Joined: Sat Mar 23, 2013 12:11 pm
Location: Just outside the asylum

Odd Caching issue

Thu Jun 10, 2021 5:43 pm

Whilst trying to work out why something wasn't working as expected, I came across another odd issue.

Pi number 1 is monitoring my door bell, and when it is pressed, it calls Pi number 2 to take a picture. The stdout/stderr output should be written to a log file, along with the date/time that the button was pressed. The oddity is that it appears to be caching this data, and then writing it next time there is a button press. The log file (ls -l) has the time stamp for the most recent event, but the last entry is for the event before that.

Code: Select all

from gpiozero import Button
import subprocess
import sys
import time

doorbell_sensor = Button(6)

while True:
    doorbell_sensor.wait_for_press() 

    date_string = time.strftime ('%y%m%d%H%M')

    time_log = open ('/home/pi/data/door_log.txt', 'a+')
    time_log.write (date_string + "\n")

    # calls pi2 to take photo etc
    process = subprocess.Popen("ssh pi2 /home/pi/bin/take_still.sh", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    output,stderr = process.communicate()

    time_log.write (output.decode())
    time_log.write (stderr.decode() + "\n")
    time_log.close

    # Wait 30 seconds before continuing
    doorbell_sensor.wait_for_release(30)

Is what I'm running. Any suggestions on how to get it to write the most recent event information ?

Thanks

Adrian
Frequently Ffoiled

User avatar
Paeryn
Posts: 3305
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Odd Caching issue

Thu Jun 10, 2021 7:14 pm

ffoiled wrote:
Thu Jun 10, 2021 5:43 pm

Code: Select all

while True:
    doorbell_sensor.wait_for_press() 

    date_string = time.strftime ('%y%m%d%H%M')

    time_log = open ('/home/pi/data/door_log.txt', 'a+')
    time_log.write (date_string + "\n")

    # calls pi2 to take photo etc
    process = subprocess.Popen("ssh pi2 /home/pi/bin/take_still.sh", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    output,stderr = process.communicate()

    time_log.write (output.decode())
    time_log.write (stderr.decode() + "\n")
    time_log.close

    # Wait 30 seconds before continuing
    doorbell_sensor.wait_for_release(30)
Your time_log.close isn't closing the file, without brackets after it you are just getting a reference to the method / function rather than calling it, it should be

Code: Select all

    time_log.close()
Files are buffered so the writing doesn't happen exactly when you write to them, the system will wait until either the file's buffer is full or it is flushed / closed. I'm pretty sure Python will close a file when there are no more references to it which will happen when you go around the loop and you reassign time_log with a new file, though Python will open the new file before it closes the old one which could cause issues.
She who travels light — forgot something.
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!

ffoiled
Posts: 52
Joined: Sat Mar 23, 2013 12:11 pm
Location: Just outside the asylum

Re: Odd Caching issue

Thu Jun 10, 2021 7:24 pm

Paeryn wrote:
Thu Jun 10, 2021 7:14 pm

Your time_log.close isn't closing the file, without brackets after it you are just getting a reference to the method / function rather than calling it, it should be

Code: Select all

    time_log.close()
Files are buffered so the writing doesn't happen exactly when you write to them, the system will wait until either the file's buffer is full or it is flushed / closed. I'm pretty sure Python will close a file when there are no more references to it which will happen when you go around the loop and you reassign time_log with a new file, though Python will open the new file before it closes the old one which could cause issues.
Thanks. I've added the brackets, and I'll give a test tomorrow.

Adrian
Frequently Ffoiled

Return to “Python”