Page 1 of 1

New user, question about script running in the background

Posted: Mon Jul 24, 2017 11:22 pm
by fangis
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?

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

Posted: Tue Jul 25, 2017 6:16 am
by pythoncoder
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.

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

Posted: Tue Jul 25, 2017 10:42 am
by fangis
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.

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

Posted: Wed Jul 26, 2017 7:11 am
by pythoncoder
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...

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

Posted: Wed Jul 26, 2017 6:08 pm
by Roberthh
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.

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

Posted: Fri Aug 11, 2017 2:21 am
by ajie_dirgantara
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?