while true in boot
while true in boot
Hello, can I have a while True loop inside boot?
I found this network config code (https://github.com/cpopp/MicroPythonSam ... kconfig.py). But if I put in boot.py the following code:
import networkconfig
networkconfig.start()
and in main.py I started another code, it wont start.
So, what could be wrong? Can't I have a while True statement in boot.py?
I found this network config code (https://github.com/cpopp/MicroPythonSam ... kconfig.py). But if I put in boot.py the following code:
import networkconfig
networkconfig.start()
and in main.py I started another code, it wont start.
So, what could be wrong? Can't I have a while True statement in boot.py?
Re: while true in boot
Boot.py and main.py are executed sequentially, not in parallel, and if main.py finishes, you'll get the REPL prompt. If boot .py does not end, main.py will not run.
So: No, you must not have a while True loop in boot.py, if you want main.py to run. If you want to run the code you refer to, put it at the end of main.py.
So: No, you must not have a while True loop in boot.py, if you want main.py to run. If you want to run the code you refer to, put it at the end of main.py.
Re: while true in boot
There is something else wrong. As you can see from your code example, you only define a few functions. They never get called. Thus, you should not expect that you reach the while true loop.
Could be as simple as a small error, which threw out some exceptions and stops execution. Hence, you main.py will never be called.
Personally, I would not add all this code to boot.py. Boot.py should be as slim as possible to be as fast as possible. Try to copy the code over to main.py and check what happens then.
Use the reply to get some feedback.
Could be as simple as a small error, which threw out some exceptions and stops execution. Hence, you main.py will never be called.
Personally, I would not add all this code to boot.py. Boot.py should be as slim as possible to be as fast as possible. Try to copy the code over to main.py and check what happens then.
Use the reply to get some feedback.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: while true in boot
To offer some general advice. It's best to keep boot.py minimal. I do the same for main.py: simply have a line
with mymodule doing the actual work. If mymodule has a bug which locks up or crashes the machine all that's required is to replace main.py with a vanilla one and debug the module at the REPL.
Why do you want to pull in code in boot.py?
Code: Select all
import mymodule
Why do you want to pull in code in boot.py?
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: while true in boot
@torwag: As far as I understand the post, these two lines are in boot.py:
so start() is called, which contains the while true loop.
Code: Select all
import networkconfig
networkconfig.start()
Re: while true in boot
Thanks for all your answers. Indeed my boot.py and main.py are very small, they have only the calls to other .py files and functions.
Thats correct @Roberthh. @pythoncoder I did as you mentioned.Roberthh wrote:@torwag: As far as I understand the post, these two lines are in boot.py:so start() is called, which contains the while true loop.Code: Select all
import networkconfig networkconfig.start()
Is there another approach? It's possible to change the code to take off the while True loop and have the same result?Roberthh wrote:Boot.py and main.py are executed sequentially, not in parallel, and if main.py finishes, you'll get the REPL prompt. If boot .py does not end, main.py will not run.
So: No, you must not have a while True loop in boot.py, if you want main.py to run. If you want to run the code you refer to, put it at the end of main.py.
Re: while true in boot
Hello @vitor, it is not clear to me what you want to achieve. Clearly, the intended use of boot.py is to set up the initial state, and may of us use main.py for just calling the final script. But that is a matter of style.
It seems to me that you want to achieve concurrent execution of some code. That could be done with the asyncio module, although that and the networkconfig,py code will most likely blow up the RAM. For asyncio, @pythoncoder will surely give you some hints on how to use it.
It seems to me that you want to achieve concurrent execution of some code. That could be done with the asyncio module, although that and the networkconfig,py code will most likely blow up the RAM. For asyncio, @pythoncoder will surely give you some hints on how to use it.
Re: while true in boot
Hi @Roberthh, My need is: turn available a way to the final user change the wlan station connection. I guess I can modify the networkconfig.py file to break the while loop if the wlan station is connected and execute the start function again if the user press a button.Roberthh wrote:Hello @vitor, it is not clear to me what you want to achieve. Clearly, the intended use of boot.py is to set up the initial state, and may of us use main.py for just calling the final script. But that is a matter of style.
It seems to me that you want to achieve concurrent execution of some code. That could be done with the asyncio module, although that and the networkconfig,py code will most likely blow up the RAM. For asyncio, @pythoncoder will surely give you some hints on how to use it.
I will wait pythoncoder hints.
Re: while true in boot
The wlan connection is changed by a single call to network.WLAN.connect(), with SSID, auth mode and password supplied.
I typically have these statements in a main.py:
If you want to change the connection later, you can just repeat that with different credentials.
I typically have these statements in a main.py:
Code: Select all
import network
import time
wlan = network.WLAN(network.STA_IF) # create station interface
wlan.active(True) # activate the interface
if not wlan.isconnected(): # check if the station is connected to an AP
wlan.connect('my_ssid', 'my_password') # connect to an AP
for _ in range (10):
if wlan.isconnected(): # check if the station is connected to an AP
break
print('.', end='')
time.sleep(1)
print('\nnetwork config:', wlan.ifconfig())
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: while true in boot
@vitor We are talking at cross purposes on the subject of "small" scripts. A boot.py containing
is not "small" in my book if the module contains hundreds of lines of code which run immediately! By "small" I mean one or two lines of Python which perform actions which are necessarily done early in the boot sequence.
By contrast main.py can legitimately import a large module which runs immediately: that is how it is used when you want a program to auto-run on boot.
As an example here is the boot.py from the Pyboard I have sitting next to me.
Code: Select all
import huge_python_module
By contrast main.py can legitimately import a large module which runs immediately: that is how it is used when you want a program to auto-run on boot.
As an example here is the boot.py from the Pyboard I have sitting next to me.
Code: Select all
import pyb
pyb.usb_mode('CDC') # act as a serial (CDC) and not a storage device (MSC)
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.