Page 1 of 1

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

Posted: Sun Aug 07, 2022 2:24 am
by twodoctors
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?

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

Posted: Sun Aug 07, 2022 5:58 am
by Roberthh
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.

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

Posted: Sun Aug 07, 2022 10:32 am
by DeaD_EyE
Why not importing the module and having a function to start and stop the functionality?

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

Posted: Sun Aug 07, 2022 11:27 pm
by twodoctors
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.

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

Posted: Mon Aug 08, 2022 12:04 am
by twodoctors
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?

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

Posted: Mon Aug 08, 2022 5:59 am
by Roberthh
In that case the code and the data it requires is simply too large and cannot be handled by a Pi Pico.

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

Posted: Mon Aug 08, 2022 7:19 am
by twodoctors
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....

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

Posted: Mon Aug 08, 2022 8:28 am
by Roberthh
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.

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

Posted: Mon Aug 08, 2022 9:03 am
by Christian Walther
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.