Thread support ESP8266 build

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
carsten
Posts: 15
Joined: Wed Aug 16, 2017 3:08 pm

Thread support ESP8266 build

Post by carsten » Wed Aug 16, 2017 3:15 pm

Hi,

so the standard ESP8266 build offered on micropython.org doesn't include the thread library. Is there a reason for that? I would just go ahead and build it myself from the github tree. I guess a normal build would include the library, as far as I can see, right?


Thanks for your help
Carsten

carsten
Posts: 15
Joined: Wed Aug 16, 2017 3:08 pm

Re: Thread support ESP8266 build

Post by carsten » Wed Aug 16, 2017 8:28 pm

Nvm, just nobody implemented it for esp8266 yet, right?

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

Re: Thread support ESP8266 build

Post by pythoncoder » Thu Aug 17, 2017 6:53 am

By all means try it but it's possible that the ESP8266 is too resource-constrained for this. I assume you've considered and rejected uasyncio (which runs fine on the ESP8266).
Peter Hinch
Index to my micropython libraries.

carsten
Posts: 15
Joined: Wed Aug 16, 2017 3:08 pm

Re: Thread support ESP8266 build

Post by carsten » Thu Aug 17, 2017 9:03 am

Well, I would if I could. I guess the implementation for another single-core MCU like the cc3200 could be quite similar, but I don't know much about the hardware yet and my skills in C are very limited.

No, I guess I'll try my luck with uasyncio, I was just figuring out all my options. Coming from desktop computers, a thread for a webserver was my obvious choice at first. As for the resources, I guess a threaded implementation wouldn't be much different, right?

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

Re: Thread support ESP8266 build

Post by pythoncoder » Fri Aug 18, 2017 6:08 am

Threading is indeed the choice on a PC. But the world of microcontrollers is very different. A small PC might have 4GB of RAM whereas 400K on a microcontroller would be pretty big. The ESP8266 is particularly small with about 26KB free. More than five orders of magnitude less than a small PC! So techniques which you would use freely on a PC may be unfeasible on a microcontroller.

Pre-emptive scheduling uses a lot of RAM because of the amount of context which has to be stored for each pending thread. It is supported on the larger MicroPython platforms but I have doubts whether the ESP8266 has sufficient RAM to support it. Cooperative scheduling, by contrast, is economical and the uasyncio implementation is particularly efficient. I have used it extensively on the ESP8266.

The bugbear tends to be blocking sockets. Using threading one thread can block without issue. With cooperative scheduling any task which blocks will prevent other tasks getting execution. So you need to write your code using nonblocking sockets.

If you're new to asyncio or unclear about the uasyncio subset, I wrote a tutorial here https://github.com/peterhinch/micropyth ... UTORIAL.md.
Peter Hinch
Index to my micropython libraries.

carsten
Posts: 15
Joined: Wed Aug 16, 2017 3:08 pm

Re: Thread support ESP8266 build

Post by carsten » Mon Aug 21, 2017 8:51 pm

Thanks for your comprehensive reply! I didn't have any experience yet, my ESP8266 arrived a week ago. You convinced me, uasyncio proved to be very convenient. I had some time to set up a captive portal to configure/manage wifi networks. Neither complete nor particularly nice yet, but maybe someone will find it useful:

https://github.com/carstenblt/micropython-configserver

Two things I noticed...
  • Btree proved to be way to memory hungry on the ESP8266. At first I wanted to store wifi passwords with it, but even just opening an empty database usually resulted in running out of memory.
  • It would be awesome if you had numpy-style sphinx documentation in the source code. (Documentation right after the module/function/class definition, usually with a detailed explanation of arguments and return values.) It pushes developers to write docs right when they code, so it stays up to date and comprehensive. Also, exploring code gets much easier. But I guess you have reasons to not use it?

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

Re: Thread support ESP8266 build

Post by pythoncoder » Tue Aug 22, 2017 11:50 am

carsten wrote:...Btree proved to be way to memory hungry on the ESP8266...[/list]
See viewtopic.php?f=16&t=3653&p=21216&hilit=btree#p21216 where people have done humungous database operations using the ESP8266.
tl;dr:

Code: Select all

db = btree.open(dbfile, pagesize=1024)
Pass on the second part of your question: the core developers will have an answer to that one. Perhaps it's down to port-specific variations? But I'm guessing here.
Peter Hinch
Index to my micropython libraries.

Post Reply