Page 1 of 2
while true in boot
Posted: Sun Apr 02, 2017 2:06 am
by vitor
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?
Re: while true in boot
Posted: Sun Apr 02, 2017 6:34 am
by Roberthh
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
Posted: Sun Apr 02, 2017 6:35 am
by torwag
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.
Re: while true in boot
Posted: Sun Apr 02, 2017 6:39 am
by pythoncoder
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?
Re: while true in boot
Posted: Sun Apr 02, 2017 6:40 am
by Roberthh
@torwag: As far as I understand the post, these two lines are in boot.py:
Code: Select all
import networkconfig
networkconfig.start()
so start() is called, which contains the while true loop.
Re: while true in boot
Posted: Sun Apr 02, 2017 12:54 pm
by vitor
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.
Roberthh wrote:@torwag: As far as I understand the post, these two lines are in boot.py:
Code: Select all
import networkconfig
networkconfig.start()
so start() is called, which contains the while true loop.
Thats correct @Roberthh. @pythoncoder I did as you mentioned.
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.
Is there another approach? It's possible to change the code to take off the while True loop and have the same result?
Re: while true in boot
Posted: Sun Apr 02, 2017 3:52 pm
by Roberthh
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.
Re: while true in boot
Posted: Sun Apr 02, 2017 5:39 pm
by vitor
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.
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.
I will wait pythoncoder hints.
Re: while true in boot
Posted: Sun Apr 02, 2017 6:05 pm
by Roberthh
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:
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())
If you want to change the connection later, you can just repeat that with different credentials.
Re: while true in boot
Posted: Mon Apr 03, 2017 6:43 am
by pythoncoder
@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 pyb
pyb.usb_mode('CDC') # act as a serial (CDC) and not a storage device (MSC)