File write speed

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
User avatar
pythoncoder
Posts: 4924
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: File write speed

Post by pythoncoder » Tue May 15, 2018 5:13 am

@goatchurch I think you may have misunderstood my above comment. I was suggesting an asynchronous solution along these lines:

Code: Select all

import uasyncio as asyncio

async def sender():
    with open('test.txt', 'w') as f:
        swriter = asyncio.StreamWriter(f, {})
        while True:
            await swriter.awrite('Hello file\n')
            await asyncio.sleep_ms(10)

loop = asyncio.get_event_loop()
loop.create_task(sender())
loop.run_forever()
(The 10ms sleep was merely to keep the file size within bounds on this fast Linux box.)
Peter Hinch

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: File write speed

Post by jickster » Tue May 15, 2018 9:02 pm

pythoncoder wrote:
Tue May 15, 2018 5:13 am
@goatchurch I think you may have misunderstood my above comment. I was suggesting an asynchronous solution along these lines:

Code: Select all

import uasyncio as asyncio

async def sender():
    with open('test.txt', 'w') as f:
        swriter = asyncio.StreamWriter(f, {})
        while True:
            await swriter.awrite('Hello file\n')
            await asyncio.sleep_ms(10)

loop = asyncio.get_event_loop()
loop.create_task(sender())
loop.run_forever()
(The 10ms sleep was merely to keep the file size within bounds on this fast Linux box.)
While this may work in general, his problem is that the ESP OS freezes the entire system when a flash write is performed to the same flash memory where the code is stored.

To overcome this, he needs to store his code in a different location than SPI flash.

User avatar
pythoncoder
Posts: 4924
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: File write speed

Post by pythoncoder » Wed May 16, 2018 5:25 am

OK, I hadn't appreciated that.
Peter Hinch

monquarter
Posts: 14
Joined: Sat Jan 11, 2020 2:31 pm

Re: File write speed

Post by monquarter » Sun Dec 13, 2020 12:25 am

I have a similar issue where I am trying to log sensor acquired data quickly, and tried the solution proposed above:

Code: Select all

import uasyncio as asyncio

async def sender():
    with open('test.txt', 'w') as f:
        swriter = asyncio.StreamWriter(f, {})
        while True:
            await swriter.awrite('Hello file\n')
            await asyncio.sleep_ms(10)

loop = asyncio.get_event_loop()
loop.create_task(sender())
loop.run_forever()
However, I get an error (testing on a Pyboard D-series with Micropython 1.13

Code: Select all

<Task>
Traceback (most recent call last):
  File "<stdin>", line 12, in <module>
  File "uasyncio/core.py", line 1, in run_forever
  File "uasyncio/core.py", line 1, in run_until_complete
  File "uasyncio/core.py", line 1, in wait_io_event
OSError: [Errno 22] EINVAL
>>>
I'm not sure if I am missing something, or if uasyncio supports writing to files. I wish it did as it would be very helpful.

Post Reply