Closing file takes time

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
kotk_o
Posts: 10
Joined: Mon Mar 22, 2021 12:19 pm

Closing file takes time

Post by kotk_o » Thu Oct 13, 2022 9:23 am

Hello Team,

If you execute this little piece of code:

Code: Select all

import utime
import machine

count=0
machine.freq(240000000)

while True:
    with open("test1.json", 'w',buffering=0) as fd:
        fd.write("hello")
        fd.flush()

    utime.sleep_ms(100)
You will see that every 89 loops, the micropython 'hangs/freezes' for a 5s !!!! to close or flush the file.

Maybe because there is a buffer between the flash and RAM, but.. how to avoid it? This is causing a big problem in our code.

Thanks!

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Closing file takes time

Post by jimmo » Thu Oct 13, 2022 12:50 pm

This forum will be made read-only soon. Please post at GitHub Discussions.

(The only reason we're keeping this open was allow replied to existing threads, I'd be curious to know why people keep opening new threads - not many people are reading this forum any more).
kotk_o wrote:
Thu Oct 13, 2022 9:23 am
You will see that every 89 loops, the micropython 'hangs/freezes' for a 5s !!!! to close or flush the file.
I don't know why it's every 89 loops... The typical ESP32 has a large amount of flash so it probably isn't LittleFS garbage collection being triggered after such a small number of files.

I ran your exact code and I see a small pause every ~121 loops, but the pause is approx 300ms, not 5000ms. These small pauses are exactly when the block erase happens. If I write each time, then it happens more often (as the block fills up faster). FWIW I'm using a ESP32_Devkit_c (with a WROVER-B module).

What ESP32 board do you have? What type of spiflash does it have? Does it have spiram?

kotk_o
Posts: 10
Joined: Mon Mar 22, 2021 12:19 pm

Re: Closing file takes time

Post by kotk_o » Thu Oct 13, 2022 2:30 pm

Hello Jimmo,

Believe me when I say that I searched in the forum before open this topic.

I found this thread:
viewtopic.php?t=4725

But from it I didn't see any information about the huge delay and I decided to open a new topic.

We have the ESP32 with spiram (16MB) and FAT system.

We made a lot of tests opening and closing the file to get the timing from it: It seems that when we call flush() or after close the file ( I think that flush is called), randomly, there is a time of 5800ms of delay after close/flush the file.

Even if we didn't write nothing, only opening and closing the file, this delay appears.

Of course, this is related with the external RAM and SPI. So the questions is:

-Is there any solution to avoid this 5s delay?

Thanks.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Closing file takes time

Post by jimmo » Thu Oct 13, 2022 2:55 pm

kotk_o wrote:
Thu Oct 13, 2022 2:30 pm
Believe me when I say that I searched in the forum before open this topic.
That wasn't my question though.... why didn't you post at GitHub Discussions?
kotk_o wrote:
Thu Oct 13, 2022 2:30 pm
We have the ESP32 with spiram (16MB) and FAT system.
Why FAT? Do you see this with littlefs?

Post Reply