async, promises ... ?

89 views
Skip to first unread message

Damien Polizzi

unread,
May 23, 2016, 7:51:05 AM5/23/16
to brython
Hello,
I'm alwas in trying to implement correctly one http ajax request, kiko helped me, it works but...

There is something that I hope to write better.
my code look like this :
class ChargeFichier() :
   
def __init__(self,url) :
       
self.url = url

   
def debutChargement(self) :

       
def on_complete(req):
           
if self.req.status in (200,304) :
               
return (self.req.text)
           
else :
               
return("error "+self.req.text)

       
self.req = ajax.ajax()
       
self.req.bind("complete", on_complete)
       
self.req.open('GET',self.url,True)
       
self.req.send()
       

   
def err_msg(self):
       
print("le serveur n'a pas répondu après %s secondes" %timeOut)



My problem, and my question is : how to get what my on_complete function return without using the dom to put the result ?

And I found the python syntax await - async witch come into python since 3.5 who could give a code like that...
c
lass ChargeFichier() :
   
def __init__(self,url) :
       
self.url = url

    async
def debutChargement(self) :

       
def on_complete(req):
           
if self.req.status in (200,304) :
               
return (self.req.text)
           
else :
               
return("error "+self.req.text)

       
self.req = ajax.ajax()
        reponse
= await on_complete(req)    
       
self.req.send()
       
return reponse....

   
def err_msg(self):
       
print("le serveur n'a pas répondu après %s secondes" %timeOut)


But the words await and async are not reconized by brython.

Then I tryed something else : using asyncio whith the @asyncio.coroutine and yield from
but unfortunatly, brython doesn't implements this library.

So I had a look to javascript and I looked for promises, but I didn't understand all about that. And I don't see very well how to implement that in brython.

Does someone have one idea ?

Eric S. Johansson

unread,
May 23, 2016, 6:12:59 PM5/23/16
to brython


On Monday, May 23, 2016 at 7:51:05 AM UTC-4, Damien Polizzi wrote:

Does someone have one idea ?

my solution lets you nest ajax calls and, I suspect, run them in parallel.  let me know what my class does not do that you need and I'll try to fit it in.  

Damien Polizzi

unread,
May 24, 2016, 3:41:47 AM5/24/16
to brython
Ok thank you eric !
I think I understand your code, I didn't see it before into the other post.
I think it's interesting to inherit from one ajax object, it rendered the thinks more natural and with less code.

But it's not exactly the same staf than the way of promises or async way.
I 'll try something in the following days in inspiring me on your staff and other things, if I manage to do something, I'll come back to publish my code !

Thank you another time

Olemis Lang

unread,
May 24, 2016, 10:36:44 AM5/24/16
to bry...@googlegroups.com
On 5/23/16, Damien Polizzi <damien....@gmail.com> wrote:
> Hello,
>
[...]



[...]
>
> My problem, and my question is : how to get what my on_complete function
> return without using the dom to put the result ?
>
> And I found the python syntax await - async witch come into python since
> 3.5 who could give a code like that...
>
[...]
>
> But the words await and async are not reconized by brython.
>

Because Brython is compatible with CPython 3.4 , the only exception
being the import machinery which is aimed at being compliant with 3.5
.

> Then I tryed something else : using asyncio whith the @asyncio.coroutine
> and yield from
> but unfortunatly, brython doesn't implements this library.
>

Brython core execution model is synchronous . There is a ticket [1]_
for implemennting async though .

[...]

.. [1] https://github.com/brython-dev/brython/issues/210

--
Regards,

Olemis - @olemislc

Apache™ Bloodhound contributor
http://issues.apache.org/bloodhound
http://blood-hound.net

Brython committer
http://brython.info
http://github.com/brython-dev/brython

Blog ES: http://simelo-es.blogspot.com/
Blog EN: http://simelo-en.blogspot.com/

Featured article:

Damien Polizzi

unread,
May 24, 2016, 10:46:15 AM5/24/16
to brython
Hello !
I begin to write something but...
class RequeteAjax () :
   
def __init__(self, url, ajaxMethod, timeOut = 20) :
       
self.url = url;
       
self.ajaxMethod = ajaxMethod;
       
self.timeOut = timeOut;


   
def err_msg(self):
       
print("le serveur n'a pas répondu après %s secondes" %self.timeOut)

   
def resoudre(self,resolve,reject) :
       
def onComplete(self,req) :
           
if req.status >200 and req.status<300 :
                resolve
("youyou")
           
else :
                reject
(req.status)
       
self.req = ajax.ajax()
       
self.req.open(self.ajaxMethod, self.url, True)
       
self.req.set_timeout (self.timeOut,self.err_msg)
       
self.req.bind ("complete", onComplete)
       
print("après bind")
       
self.req.send()
       
print("après send")

   
def chargementUrl(self) :
       
self.promesse = JSConstructor(window.Promise)(self.resoudre)

requeteAjax
= RequeteAjax("http://localhost:8888/","""GET""",4)
requeteAjax
.chargementUrl()

the begining is working well, but when we reach the onComplete function, the resolve and the reject function is returning a TypeError. The function are type of JSObject. If someone see how to proceed... I wil thank you !!!

see the error :
Saisissez le code ici..._b_.TypeError()
 brython
.js line 6106 > eval:121
$locals
.onComplete</<()
 brython.js line 4099 > eval:193
ajax/
xmlhttp.onreadystatechange()



After I'll have to extract the value of the promise and the code will be good...
Message has been deleted
Message has been deleted

Damien Polizzi

unread,
May 24, 2016, 11:45:51 AM5/24/16
to brython
Thanks Oemis for your answer !!!
I'm happy that there is a ticket to implement async fonction. I hope really that you or someone in the team will have the time to implement it !

I've manage to implement a promise, but I don't think that it will do what I wanted and the async - await is a really better way.

If you want the code, see it below
class RequeteAjax () :
   
def __init__(self, url, ajaxMethod, timeOut = 20) :
       
self.url = url;
       
self.ajaxMethod = ajaxMethod;
       
self.timeOut = timeOut;



   
def err_msg(self):
       
print("le serveur n'a pas répondu après %s secondes" %self.timeOut)

   
def resoudre(self,resolve,reject) :
       
def onComplete(req) :

           
if req.status >200 and req.status<300 :

                resolve
(req.response)
           
else :
               
print("reject")

                reject
(req.status)
       
self.req = ajax.ajax()
       
self.req.open(self.ajaxMethod, self.url, True)
       
self.req.set_timeout (self.timeOut,self.err_msg)
       
self.req.bind ("complete", onComplete)

       
self.req.send()


   
def chargementUrl(self) :
       
self.promesse = JSConstructor(window.Promise)(self.resoudre)

       
self.promesse.then(self.ok)
       
self.promesse.catch(self.erreur)
   
def ok(self,donnees) :
       
print("ok")
   
def erreur(self,erreur) :
       
print("erreur")

Damien Polizzi

unread,
May 24, 2016, 11:51:16 AM5/24/16
to brython
I've just omitted the code to call the request :
Reply all
Reply to author
Forward
0 new messages