New user, question about script running in the background

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
fangis
Posts: 11
Joined: Mon Jul 24, 2017 11:03 pm

New user, question about script running in the background

Post by fangis » Mon Jul 24, 2017 11:22 pm

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?

User avatar
pythoncoder
Posts: 3421
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: New user, question about script running in the background

Post by pythoncoder » Tue Jul 25, 2017 6:16 am

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.
Peter Hinch

fangis
Posts: 11
Joined: Mon Jul 24, 2017 11:03 pm

Re: New user, question about script running in the background

Post by fangis » Tue Jul 25, 2017 10:42 am

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.

User avatar
pythoncoder
Posts: 3421
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: New user, question about script running in the background

Post by pythoncoder » Wed Jul 26, 2017 7:11 am

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

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

Re: New user, question about script running in the background

Post by Roberthh » Wed Jul 26, 2017 6:08 pm

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:

Code: Select all

    ftpsocket.setsockopt(socket.SOL_SOCKET, 20, accept_ftp_connect)
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.

ajie_dirgantara
Posts: 79
Joined: Fri Sep 02, 2016 9:26 am

Re: New user, question about script running in the background

Post by ajie_dirgantara » Fri Aug 11, 2017 2:21 am

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?

Post Reply