Page 1 of 1

Are file writes non blocking?

Posted: Wed Jan 19, 2022 11:01 am
by KJM
I've got a problem with

Code: Select all

with open('update', 'w') as f: f.write(update); print('update stored'); machine.deepsleep(900*1000)
Where update is a 29k text file. The problem is the update file is created in flash but has nothing in it. I don't understand how the print can output if the write hasn't happened?

I tried allowing some extra time for the write to occur with

Code: Select all

with open('update', 'w') as f: f.write(update); time.sleep(1); print('update stored'); machine.deepsleep(900*1000)
but same result, the appearance of a zero size update file in os.listdir() after the program has run.

If I move the write a few lines further up before the deeplseep it works properly but I don't understand why. Can anyone suggest what's going on?

Re: Are file writes non blocking?

Posted: Wed Jan 19, 2022 4:28 pm
by scruss
write needs a flush or the passage of time to make them happen

Re: Are file writes non blocking?

Posted: Wed Jan 19, 2022 5:05 pm
by Roberthh
Try to write the code a little bit different.

Code: Select all

with open('update', 'w') as f:
    f.write(update);
print('update stored');
machine.deepsleep(900*1000)
As far as In understand, everything behind the : after with or if or ... is considered as a single statement. And since that includes deepsleep(), the automatic flush() after the with statement does not happen.

Re: Are file writes non blocking?

Posted: Wed Jan 19, 2022 6:25 pm
by davef
Even a print() statement before lightsleep() or deepsleep() needs a wait or you only get part of it.

Code: Select all

    print ('going to lightsleep() for the timer duration')
    utime.sleep(.5) #  so print() works
    machine.lightsleep(5 * 1000)

Re: Are file writes non blocking?

Posted: Wed Jan 19, 2022 10:35 pm
by KJM
Looks like I'm a victim of my penchant for multi cmd lines! If I limit myself to 1 cmd per line I end up doing a LOT of scrolling due code dev. Is there a way around this or do I need to get a bigger monitor & set it up in portrait mode?

Re: Are file writes non blocking?

Posted: Wed Jan 19, 2022 11:29 pm
by dhylands
How are you checking the file size?

I don't think that closing the file means that it's written to the filesystem by the time that the close returns.

I think you would need to call os.sync() to ensure that everything has actually been committed to flash. deepsleep is like a reset, so if the data was sitting in memory in a cache, it's gone by the time deepsleep returns since deepsleep is like a reset.

Re: Are file writes non blocking?

Posted: Fri Jan 21, 2022 12:26 am
by KJM
I gave that a try in an attempt to force a flush

Code: Select all

with open('update', 'w') as f: f.write(update)                                                                                                      
os.sync()
but I must be doing it wrong?

Code: Select all

line 101 AttributeError: module object has no attribute sync

Re: Are file writes non blocking?

Posted: Fri Jan 21, 2022 10:11 am
by Roberthh
you can use f.flush(), or simply leave the with() code block after with. That will close the file, and on file close the buffer will be flushed as well. So if you write your code in proper style, everything should go well.

Code: Select all

with open('update', 'w') as f:
	f.write(update)                                                                                                      
You problem is, that the deepsleep() call is on the same code line as the with(). Then, the with code block was not left before deepsleep, and the intrinsic flush and close did not happen.
Edit: The ';' in a statement list binds stronger than the colon ':'. So the statement list after 'with' is treated as a single statement.