Making async call to sync call

59 views
Skip to first unread message

Clavier

unread,
Nov 30, 2013, 7:34:22 PM11/30/13
to pyjs-...@googlegroups.com
Hi,

I found that Pyjs supported only async remote call (ex: HTTPRequest.AsyncPost(), but not HTTPRequest.Post()).  It makes source code complicated when a series of remote calls are using preceding results.  Many applications use a function call returning a value, but an async call forces to separate the function and the return value making code complicated. This is an example using xmlrpclib in Python:

    import xmlrpclib
    s = xmlrpclib.ServerProxy('http://localhost:8000')
    v1=s.pow(2,3)  # pow() is a remote function
    v2=s.add(v1,3) # add() is a remote function 
    v3=s.div(v2,2) # div() is a remote function

Corresponding code for Pyjs is a lot more complicated (sudo code):

    s = JSONProxy('http://localhost:8000')
    class ServiceHandler:
        def onRemoteResponse(self, response, sender):
            if sender=='pow':
                self.return_of_pow=response
                s.add(self.return_of_pow, 3, self)
            if sender=='add':
                self.return_of_add=response
                s.div(self.return_of_add, 2, self)
            if sender=='div':
                self.return_of_div=response  
    handler=ServiceHandler()
    s.pow(2,3, handler)

The return values from the server are not directly passed, but asynchronously passed to the event handler.  It is nature of asynchronous programming making code complicated. It is possible to make code simpler using 'yield' command and generators, and this is how I am writing code in Pyjs now. But this asynchronous programming style has a problem making debugging more difficult because stack contents cannot be shown properly. 

So, I want to use synchronous calls like xmlrpclib of Python.  It can cause GUI interface to get stuck sometimes, but it is ok in most of applications. In order to implement it in Pyjs, inserting a code waiting server response is enough, but the control should go to the Pyjs main loop because Pyjs cannot updates server response while user code is running.  So, I needed a function to force Pyjs to update server response. 

      HTTPRequest.asyncPost('http://localhost:8000', handler)
      while handler.did_server_responded!=True:        #waiting server response
          force_Pyjs_to_update_events()

Is there such a function in Pyjs? If not, I'd like to try to make one. Please let me know if you have an idea.

Lex Berezhny

unread,
Nov 30, 2013, 7:42:30 PM11/30/13
to pyjs-...@googlegroups.com
I think your time would be much better spent just learning to use asynchronous programming.

If you insist on thinking synchronously then I'd suggest you look at Meteor and their reactive programming model. That would be a more flexible approach to bring to Pyjs than your blocking method.

 - lex


--
 
---
You received this message because you are subscribed to the Google Groups "Pyjs.org Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyjs-users+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Clavier

unread,
Dec 1, 2013, 7:31:25 AM12/1/13
to pyjs-...@googlegroups.com
I already implemented my application using asynchronous programming with generators and yield.  It is running well in Pyjd, but it dies in Pyjs.  I think that the implementation of generator (or yield) in Pyjs has a problem.  Pyjs throws an error right after g.send():

    asyncEngine.py, line 82:
        g_next=g.send(g_next)
    GUI.py, line 200:
        self.addOutput(text)
    SCRIPT5007: Unable to get property '__object' of undefined or null reference

It is not a problem of the line "self.addOutput(text)" because the error doesn't disappear even if I delete (or change) the line, and it runs well in Pyjd.  Another problem of asynchronous programming is that it is too difficult to debug code.  I will keep trying to debug, but I don't think that I can fix this bug in Pyjs.  So, I have no choice but looking at another method other than asynchronous programming now.

Thanks for letting me know about Metero and reactive programming.  I will try to find a way to bring this technique in Pyjs. However, I still think that my blocking method is the simplest way though not perfect.

Thanks again.

Clavier

unread,
Dec 11, 2013, 6:42:17 AM12/11/13
to pyjs-...@googlegroups.com
I found that "future" of python took care of a synchronous call too.  Following is an example if HTTPRequest supported it.

    futureObj=HTTPRequest.AsyncPost()
    value=futureObj.result()

futureObj.result() waits until the result is available. It blocks the main loop, but it is ok in many cases. It is pretty simple and exactly what I wanted.  Does Pyjs have any plan to support "future"?
Reply all
Reply to author
Forward
0 new messages