how to easiest integrate crossbar.io into existing python apps (1)

#1

Hi,

I’d like to integrate crossbar io into some of my existing python applications.
All of them are quite different but none of then is a twisted / asyncio application.

simple use cases for some simple scripts that I’m most interested in.

  1. some scripts just want to publish events
  2. some scripts want to perform a few simple, blocking rpc calls, retrieve the result and continue their work
  3. some scripts just want to be triggered by a publish (one shot) wait till result is back. and continue

All these scripts are very small and it would be great to achieve this small and limited crossbar use cases without
websockets or twisted / asyncio event loops.
it would be preferable if everything could be achieved with simple libraries like requests / crossbarconnect.

1.) and 2.) seem to be simple to achieve, but I’m stuck with 3.)

3.) subscribe and wait for a publish:

My first idea is to add a long polling url to crossbar and to use requests to initiate a subscribe event. then it could use requests to poll for an incoming event.
Unfortunately the online doc seems incomplete of how to achieve this.

A second idea (though a little more complicated) would be to start one thread, which would

  • create an autobahn.asyncio.wamp.ApplicatonSession()
  • create an autobahn.asyncio.wamp.ApplicationRunner() , which would start the ApplicationSession
    • this session would just perform one subscribe and register a callback.
    • the callback would use something like threading.Event to unblock the intial thread
      I will try this soon as this will probably work, but seems much heavier than simple code with long polling, if I manged to get it work

Does anybody have some simple example code for such a scenario?

PS.
1.) is already working with a publish url in crossbar and the crossbarconnect module.
2.) is what I’ll try next (with a caller url in crossbar and a JSON post request
3.) as mentioned. No idea what’s the simplest way to achieve this (subscribe, wait for publish, get result and continue)

Thanks in advance for any suggestions.

0 Likes

#2

Please see what I have so far:
publish and call are easily working with the http bridge and with the
crossbarconnect and requests module.

subscribe is the next one I’d like to get work without asyncio / twisted.

Anyone has feedback imporvement suggestions?

Would it make sense to add some of below functionality into crossbarconnect?
If yes I could integrate it and post on github.

import requests
import crossbarconnect

class XBTinyClient(object):
“”" Tiny Crossbar client for simple small applications
Good if a small script just needs o publish / rpc-call
a few things via crossbar
“”"
publish_path = “publish” # relative url path for publish
call_path = “call” # relative url path call
lp_path = “lp” # relative url path for long polling

def __init__(self, hostname, realm='realm1'):
    cls = self.__class__
    self.host, portstr = (hostname.split(':')  + [ 8080 ])[:2]
    self.port = int(portstr)
    self.realm = realm
    self.publish_url = "http://%s:%s/%s" % (host, portstr, cls.publish_path)
    self.call_url = "http://%s:%s/%s" % (host, portstr, cls.call_path)
    self.stop_request = False

def publish(self, ns, args):
    client = crossbarconnect.Client(self.publish_url)
    client.publish(ns, args)

def call(self, fnc, args):
    headers = { 'Content-Type' : 'application/json' }
    data = json.dumps(dict(
        procedure=fnc,
        args=args,
    ))
    r = requests.post(self.call_url, data=data, headers=headers)
    data = r.json()
    return data

def poll(self, ns, callback):
    """ blocking function to subscribe and wait for an event
        This should be possible, but I don't know how
    """
    # subscribe via long polling
    while not self.stop_request:
        pass
        # long polling request
        # if request OK, get params and call callback

def offer_func(self, func):
    """ blocking function register and handle rpc calls
        Not even sure this could really work
    """
    # register via long polling
    while not self.stop_request:
        pass
        # long polling request
        # if request OK, get call func and send back result

def stop(self):
    """ stops any ongoing poll / offer_func """
···

On Friday, May 22, 2015 at 12:44:34 AM UTC+2, Gelonida Gel wrote:

Hi,

I’d like to integrate crossbar io into some of my existing python applications.
All of them are quite different but none of then is a twisted / asyncio application.

simple use cases for some simple scripts that I’m most interested in.

  1. some scripts just want to publish events
  2. some scripts want to perform a few simple, blocking rpc calls, retrieve the result and continue their work
  3. some scripts just want to be triggered by a publish (one shot) wait till result is back. and continue

All these scripts are very small and it would be great to achieve this small and limited crossbar use cases without
websockets or twisted / asyncio event loops.
it would be preferable if everything could be achieved with simple libraries like requests / crossbarconnect.

1.) and 2.) seem to be simple to achieve, but I’m stuck with 3.)

3.) subscribe and wait for a publish:

My first idea is to add a long polling url to crossbar and to use requests to initiate a subscribe event. then it could use requests to poll for an incoming event.
Unfortunately the online doc seems incomplete of how to achieve this.

A second idea (though a little more complicated) would be to start one thread, which would

  • create an autobahn.asyncio.wamp.ApplicatonSession()
  • create an autobahn.asyncio.wamp.ApplicationRunner() , which would start the ApplicationSession
    • this session would just perform one subscribe and register a callback.
    • the callback would use something like threading.Event to unblock the intial thread
      I will try this soon as this will probably work, but seems much heavier than simple code with long polling, if I manged to get it work

Does anybody have some simple example code for such a scenario?

PS.
1.) is already working with a publish url in crossbar and the crossbarconnect module.
2.) is what I’ll try next (with a caller url in crossbar and a JSON post request
3.) as mentioned. No idea what’s the simplest way to achieve this (subscribe, wait for publish, get result and continue)

Thanks in advance for any suggestions.

0 Likes

#3

Although I haven’t personally used it, https://pypi.python.org/pypi/crochet is a library intended to help threaded/synchronous applications work with Twisted. This seems like a good place to start.

So doing a method-call would look something like: result = crochet.wait_for(session.call(“some.method”, 42))

···

On Thursday, 21 May 2015 16:44:34 UTC-6, Gelonida Gel wrote:

I’d like to integrate crossbar io into some of my existing python applications.
All of them are quite different but none of then is a twisted / asyncio application.


meejah

0 Likes

#4

Hi Gelonida,

There are 2 facilities in Crossbar.io to subscribe to a topic and receive events without using a full-fledged WAMP client:

http://crossbar.io/docs/HTTP-Bridge-Services-Subscriber/
https://github.com/crossbario/crossbarexamples/tree/master/rest/subscriber

http://crossbar.io/docs/WAMP-Long-Poll-Service/
https://github.com/crossbario/crossbarexamples/tree/master/longpoll

The former will forward an event by issuing a HTTP/POST against your script listening for incoming HTTP.

The latter will allow you to use an outgoing HTTP/POST for long polling for events.

Yet another option is Crochet (what meejah mentioned already).

Hope this helps …

Cheers,
/Tobias

···

Am Freitag, 22. Mai 2015 00:44:34 UTC+2 schrieb Gelonida Gel:

Hi,

I’d like to integrate crossbar io into some of my existing python applications.
All of them are quite different but none of then is a twisted / asyncio application.

simple use cases for some simple scripts that I’m most interested in.

  1. some scripts just want to publish events
  2. some scripts want to perform a few simple, blocking rpc calls, retrieve the result and continue their work
  3. some scripts just want to be triggered by a publish (one shot) wait till result is back. and continue

All these scripts are very small and it would be great to achieve this small and limited crossbar use cases without
websockets or twisted / asyncio event loops.
it would be preferable if everything could be achieved with simple libraries like requests / crossbarconnect.

1.) and 2.) seem to be simple to achieve, but I’m stuck with 3.)

3.) subscribe and wait for a publish:

My first idea is to add a long polling url to crossbar and to use requests to initiate a subscribe event. then it could use requests to poll for an incoming event.
Unfortunately the online doc seems incomplete of how to achieve this.

A second idea (though a little more complicated) would be to start one thread, which would

  • create an autobahn.asyncio.wamp.ApplicatonSession()
  • create an autobahn.asyncio.wamp.ApplicationRunner() , which would start the ApplicationSession
    • this session would just perform one subscribe and register a callback.
    • the callback would use something like threading.Event to unblock the intial thread
      I will try this soon as this will probably work, but seems much heavier than simple code with long polling, if I manged to get it work

Does anybody have some simple example code for such a scenario?

PS.
1.) is already working with a publish url in crossbar and the crossbarconnect module.
2.) is what I’ll try next (with a caller url in crossbar and a JSON post request
3.) as mentioned. No idea what’s the simplest way to achieve this (subscribe, wait for publish, get result and continue)

Thanks in advance for any suggestions.

0 Likes