Problem with exec(open(filename).read())

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
twodoctors
Posts: 19
Joined: Tue Jun 28, 2022 11:24 am

Problem with exec(open(filename).read())

Post by twodoctors » Sun Aug 07, 2022 2:24 am

Hi all,

I'm back with more problems!

I've trying to write a file_launcher programme so I can open different programmes with my Pi Pico using a 1.3" LCD screen.

I'm getting memory allocation error when trying to launch my files. I don't think my code in the file_launcher is crucial, but this is the command I'm using:

Code: Select all

exec(open(filename).read())
and I'm getting:

Code: Select all

Traceback (most recent call last):
  File "<stdin>", line 300, in <module>
  File "<stdin>", line 278, in launch
  File "<string>", line 161, in <module>
  File "<string>", line 29, in __init__
MemoryError: memory allocation failed, allocating 115200 bytes
1st two lines is part of my file_launcher code. 2nd two lines are the programme I'm trying to launch. In another example:

Code: Select all

Traceback (most recent call last):
  File "<stdin>", line 300, in <module>
  File "<stdin>", line 278, in launch
  File "<string>", line 13, in <module>
  File "lcd_driver.py", line 28, in __init__
MemoryError: memory allocation failed, allocating 115200 bytes
and with a smaller file:

Code: Select all

Traceback (most recent call last):
  File "<stdin>", line 300, in <module>
  File "<stdin>", line 278, in launch
MemoryError: memory allocation failed, allocating 612 bytes
This last one is just reading temp from the board, so very few lines of code.

And of course they all work fine if launched from scratch, just not via a shell that is my file_launcher.

Is there a better way of doing this? Or have I done something wrong? Perhaps I've loading the LCD driver twice by launching it via a shell?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Problem with exec(open(filename).read())

Post by Roberthh » Sun Aug 07, 2022 5:58 am

I'm a little bit surprised by the number of 115200 bytes which could not be allocated. 115200 is the baud rate. With such a message, I would look for an error in the file_launcher code.

DeaD_EyE
Posts: 19
Joined: Sun Jul 17, 2022 12:57 pm
Contact:

Re: Problem with exec(open(filename).read())

Post by DeaD_EyE » Sun Aug 07, 2022 10:32 am

Why not importing the module and having a function to start and stop the functionality?

twodoctors
Posts: 19
Joined: Tue Jun 28, 2022 11:24 am

Re: Problem with exec(open(filename).read())

Post by twodoctors » Sun Aug 07, 2022 11:27 pm

DeaD_EyE wrote:
Sun Aug 07, 2022 10:32 am
Why not importing the module and having a function to start and stop the functionality?
I'm try to create a launcher so I can run all mini games and programmes without plugging into a pc.
Roberthh wrote:
Sun Aug 07, 2022 5:58 am
LI would look for an error in the file_launcher code.
I'll do more test and see what's gone wrong. I've used the same method on another programme before and it worked fine.

twodoctors
Posts: 19
Joined: Tue Jun 28, 2022 11:24 am

Re: Problem with exec(open(filename).read())

Post by twodoctors » Mon Aug 08, 2022 12:04 am

So a bit more googling reminds me that 115200 bytes happens to be 240x240 pixels x 2-byte colour = 115200 bytes. I think the framebuffer is full with the launcher programme, and can't load another programme as there's no room.

So how do I flush the framebuffer?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Problem with exec(open(filename).read())

Post by Roberthh » Mon Aug 08, 2022 5:59 am

In that case the code and the data it requires is simply too large and cannot be handled by a Pi Pico.

twodoctors
Posts: 19
Joined: Tue Jun 28, 2022 11:24 am

Re: Problem with exec(open(filename).read())

Post by twodoctors » Mon Aug 08, 2022 7:19 am

Roberthh wrote:
Mon Aug 08, 2022 5:59 am
In that case the code and the data it requires is simply too large and cannot be handled by a Pi Pico.
Perhaps so. The same code works for my other programme but it was driving a 2004 LCD screen. Tht 240x240 pixels are probably a bit too much.

Oh well....

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Problem with exec(open(filename).read())

Post by Roberthh » Mon Aug 08, 2022 8:28 am

You could try to change the screen driver in using a color palette instead of 16 bit color values. For instance if you limit yourself to 16 colors, you can use 4 bits per pixel, reducing the size of the framebuffer by a factor of 4.

Christian Walther
Posts: 169
Joined: Fri Aug 19, 2016 11:55 am

Re: Problem with exec(open(filename).read())

Post by Christian Walther » Mon Aug 08, 2022 9:03 am

Try using __import__() instead of exec() to run the other program. I don’t know if that uses less memory but it could be. You’ll have to remove it from sys.modules afterward so it can be run again. Example.

Also, it’s hard to tell from afar, but I expect you should be able to deinitialize the display and release all references to it so the framebuffer can be reclaimed before you launch the other program.

Post Reply