Python pulsar 0.9.0 - XML-RPC server using Twisted

20 views
Skip to first unread message

vana...@ucar.edu

unread,
Sep 27, 2014, 6:19:50 PM9/27/14
to python...@googlegroups.com
I’m very new to python-pulsar, so please bear with me.

I'm trying to use the Twisted compatibility mode of pulsar, since this

is the only way I know of to get a XML-RPC server running under
asyncio.

Using Python 2.7, the attached script exits.

If I comment out the following lines:
from pulsar.apps.tx import tx
from pulsar.utils.log import process_global

and add a reactor.run() call to main(), the script runs my XML-RPC
server, with repeated calls to 'doPoll()', as I expect.

I'd really appreciate any advice on what I'm doing wrong.   

I read the source for apps/tx/__init__.py, and I see a call to _reactor.run(), which should cause the reactor to run.

Questions:
1) What am I missing in my demo program?  I’m assuming that I need to invoke the Pulsar main loop/reactor, but how do I do this?

2) If have two different files in my application that both import pulsar.apps.tx, won’t this cause two calls to _reactor.run()?



Thanks in advance.
test_xmlrpc_server.py

lsbardel

unread,
Sep 28, 2014, 4:08:04 AM9/28/14
to python...@googlegroups.com
Hi,

On Saturday, September 27, 2014 11:19:50 PM UTC+1, vana...@ucar.edu wrote:
I’m very new to python-pulsar, so please bear with me.

I'm trying to use the Twisted compatibility mode of pulsar, since this

is the only way I know of to get a XML-RPC server running under
asyncio.

To write a XML-RPC server for asyncio shouldn't be too difficult when using pulsar.
Pulsar has a JSON-RPC wsgi middleware which is not dissimilar.

All you need is a XMLRPC class inheriting from RpcHandler and implement the callable method.

from pulsar.apps.rpc import RpcHandler

class XMLRPC(RpcHandler):

   
def __call__(self, request):
       
...
 
I'm happy to help you out if you decide to go for this solution.
Once you have the XMLRPC handler you can write your application like

from pulsar.apps import wsgi

class 
MyRpc(XMLRPC):
    
    
def rpc_add(self, request, a, b):
        
return a+b


class Wsgi(wsgi.LazyWsgi):

    def handler(self, environ=None):
        app = wsgi.Router('/', post=MyRpc())
        return wsgi.wsgiHandler([app])


if __name__ == '__main__':
    wsgi.WsgiServer(Wsgi()).start()

If you want to re-use twisted implementation you can try with pulsar.apps.tx as you mentioned but I'm not sure it will work.


If I comment out the following lines:
from pulsar.apps.tx import tx


This is a crucial import. It replaces the the twisted reactor with a proxy to the asyncio event loop.
If you don't use it you are not using asyncio but twisted instead.
 

from pulsar.utils.log import process_global

You don't need this import 
 


and add a reactor.run() call to main(), the script runs my XML-RPC
server, with repeated calls to 'doPoll()', as I expect.

reactor.run is switched off by pulsar when importing tx.
To run the event loop you either use a pulsar application as above or asyncio directly

asyncio.get_eventloop().run_forever()



2) If have two different files in my application that both import pulsar.apps.tx, won’t this cause two calls to _reactor.run()?

Yes but reactor.run does nothing as previously mentioned. 

Reply all
Reply to author
Forward
0 new messages