urequests delay

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
MorseIot
Posts: 25
Joined: Fri Jun 29, 2018 12:32 am

urequests delay

Post by MorseIot » Sun Jul 08, 2018 3:29 am

When using the code example below:

Code: Select all

import time
import network
import urequests

def setNetworkConfig(ssid, pwd):
    wlan = network.WLAN(network.STA_IF)
    if not wlan.isconnected():
        print('connecting to network...')
        wlan.active(True)
        wlan.connect(ssid, pwd)
        while not wlan.isconnected():
            pass
    print('network config:' ,wlan.ifconfig())

def resourceLastFeeds(protocol,url,keyPublic,keySecret,id,silent=False):

    # defining the api-endpoint
    API_ENDPOINT = protocol+"://"+url+"/api/device/"+keyPublic+"/resource/"+str(id)+"/feeds/last"

    #Headers
    if keySecret is not None:
        headers = {"Authorization":"Bearer "+keySecret,"Content-Type":"application/json"}
    else:
        headers = {"Content-type":"application/json"}

    start = time.ticks_ms()  # get millisecond counter
    # sending get
    response = urequests.get(url = API_ENDPOINT,  headers=headers)
    parsed = response.json()
    response.close()
    delta = time.ticks_diff(time.ticks_ms(), start)
    print("Delay (ms): ",delta)
    return

setNetworkConfig('SSID','PWD')
resourceLastFeeds('http', 'automacao-iot.com.br', 'C8DE1A17E97BB0D363FF6FE5E40EC544','0502B74AC1B075AAC292F7D7FE3C27936E2F5E1E539E7F6A122164061A1469F6', 13)
I registered a delay of 5 seconds, from the request to the closing of the same (urequests - self._cached = self.raw.read())
Commenting on the "#parsed = response.json()" line, the delay passes to 360 ms (same for response.text or response.content)
Reading the urequests library, I noticed that the code of lib urequests, referring to "content" "text" and "json" when used, are responsible for this delay .

Code: Select all

os.uname()
(sysname='esp8266', nodename='esp8266', release='2.2.0-dev(9422289)', version='v1.9.4-8-ga9a3caad0 on 2018-05-11', machine='ESP module with ESP8266')
Is there any alternative to decrease this delay, using response.text, response.json or response.content ??
Am I doing something wrong?

Post Reply