Hi, I'm a new user.
I received my esp recently and flashed micropython on it. After about a week of reading the forum, it's working great: I got working telnet and ftp servers made by other users (thanks) and could write some scripts to save my external ip to a server, and to gather some info from my router about who is connected to the network.
However one last step I couldnt understand yet, it's how to make my script run in the background. I know, there is the uasyncio module, and I have tried to learn with the examples and with the tutorial, but so far I can't understand it. What i want to do seems to be simple: there is a loop like this:
while True:
time.sleep(3600)
function.start()
Which I run on main.py. I would like to have it run in the background, so that I can access the REPL without having to press control-C and interrupt this loop.
Can anyone help me?
New user, question about script running in the background
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: New user, question about script running in the background
Python (and hence MicroPython) doesn't work like that. The REPL only appears when a user program terminates. There is no way to run a user program in the background.
A (u)asyncio application supports multiple coroutines with execution passing between them at defined points in each coroutine's code. But, like other applications, the REPL won't appear until the program terminates.
A (u)asyncio application supports multiple coroutines with execution passing between them at defined points in each coroutine's code. But, like other applications, the REPL won't appear until the program terminates.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: New user, question about script running in the background
Thanks for you reply.
So, there is really no way? To me it looks like the telnet and the uftpd scripts do manage to run in the background while i can interact with the REPL.
So, there is really no way? To me it looks like the telnet and the uftpd scripts do manage to run in the background while i can interact with the REPL.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: New user, question about script running in the background
OK, I stand corrected. My above comment is true for normal Python modules but these guys have found a way to do socket programming with background execution. I'll study the code and try to figure out how the trick is performed...
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: New user, question about script running in the background
the trick of telnet and uftpd is a special non-standard callback, that can be set up for inciming socket activities. It was introduced by Paul for webrepl. Once you have a socket created, you call something like:
where accept_ftp_connect if the function which then is call at each socket event. Very basic and a little bit risky. The limitations here in README.md should be noted: https://github.com/robert-hh/ESP8266-FTP-Server. And it's limited to socket callbacks. But unlike ISR's one can allocate memory.
One may be able to do something like that with timer callbacks, but these may be even more limited in what you can do in the callback.
Code: Select all
ftpsocket.setsockopt(socket.SOL_SOCKET, 20, accept_ftp_connect)
One may be able to do something like that with timer callbacks, but these may be even more limited in what you can do in the callback.
-
- Posts: 81
- Joined: Fri Sep 02, 2016 9:26 am
Re: New user, question about script running in the background
Ok,
basically I had same requirement with ts, as I need to do .py script replacement/update on the fly, while the main application is running. The plan is, I want to replicate how rshell do this (with serial comm), so I won't create another communication protocol. Is this the correct way? or is there any better way?
basically I had same requirement with ts, as I need to do .py script replacement/update on the fly, while the main application is running. The plan is, I want to replicate how rshell do this (with serial comm), so I won't create another communication protocol. Is this the correct way? or is there any better way?