Asynchronous requests

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
Oct_opus
Posts: 3
Joined: Sat Aug 10, 2019 4:44 pm

Asynchronous requests

Post by Oct_opus » Sat Aug 10, 2019 5:02 pm

Hello everyone,

I have recently started making a smart watch project using the ESP32 board with OLED using Micropython.
https://github.com/Oct-opus/Ayon

When adding GET requests to fetch from Google Drive, I found out about network requests blocking the OLED from updating.
I was happy to change my code and use the uasyncio lib and call it a day :lol: ... but sadly it seems that uasyncio still uses blocking requests.

I've looked into peterhinch/micropython-mqtt and his implementation of non-blocking sockets but it seems to be turned around connecting to a server and not make REST calls.
In the uasyncion repo, they talk about making non-blocking socket calls and share this and example.
https://github.com/peterhinch/micropyth ... onblock.py

I don't know how I could use the concept in a rewrite of the urequeust module.
Would you happen to know of a solution or what I could implement?

Thank you.

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

Re: Asynchronous requests

Post by Roberthh » Sat Aug 10, 2019 7:41 pm

You could try threads.

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

Re: Asynchronous requests

Post by pythoncoder » Sun Aug 11, 2019 6:43 am

The is this approach which devolves the networking code to a more capable device such as a Raspberry Pi on the wired network. Communication between the Pi and your watch is by an asynchronous socket-like exchange of messages.
Peter Hinch
Index to my micropython libraries.

Oct_opus
Posts: 3
Joined: Sat Aug 10, 2019 4:44 pm

Re: Asynchronous requests

Post by Oct_opus » Sun Aug 11, 2019 3:54 pm

Thank you for your responses.

I'm not really looking into using another device alongside the smart watch project as it would only add complexity.
Currently I can turn my phone into a wifi hotspot and use it as is.
I could maybe in a second time add bluetooth to the project and the make a mobile native app to communicate with.

I have tried to uses treads but I get a [Errno 12] ENOMEM from the network request.
Weirdly it only happens when I call the google API and not the weather or clock one. It might be the duration of POST call witch causes it.

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

Re: Asynchronous requests

Post by Roberthh » Sun Aug 11, 2019 7:04 pm

ENOMEM is a memory error. Did you try to increase the stack size for the thread?

Oct_opus
Posts: 3
Joined: Sat Aug 10, 2019 4:44 pm

Re: Asynchronous requests

Post by Oct_opus » Mon Aug 12, 2019 12:48 pm

Thank you for the info.
Roberthh wrote:
Sun Aug 11, 2019 7:04 pm
ENOMEM is a memory error. Did you try to increase the stack size for the thread?
I tried the _thread.stack_size method but I still get the same error.
It's weird because even if I only put a useless function like this inside a thread and everything outside of it the network call still doesn't work.

Code: Select all

def test():
	while True:
		print("Alive")
		utime.sleep(1)
I also do not understand why my other network calls are working fine but I'm only having trouble with Google API :(

Post Reply