gevent alternative in autobhan?

#1

Hi,

in order to do a poller
i am spawning a greenlet from autobhan.
But when i call the kill method the greenlet doesn’t exit.
Is there an alternative using autobahn ?
My code :
#!/usr/bin/env python
import geventreactor; geventreactor.install()
from twisted.internet import reactor, defer
from twisted.internet.defer import inlineCallbacks
from autobahn.twisted.wamp import ApplicationSession
from autobahn.wamp.exception import ApplicationError
from autobahn import wamp
import gevent
import signal

class Component(ApplicationSession):
“”"
An application component providing procedures with
different kinds of arguments.
“”"
@inlineCallbacks
def ping(self, host, delay=3):
while True:
ping_result = yield self.call(u’com.pinger.ping’, host)
print ping_result
gevent.sleep(delay)

@inlineCallbacks
def onJoin(self, details):
    self.hostlist=set()
    self.worker={}
    yield self.register(self)

@wamp.register(u'com.poller.add')
def add(self, host):
    print 'adding  host : %s ' % host
    self.hostlist.add(host)
    self.worker[host] = gevent.Greenlet(self.ping, host)
    self.worker[host].start()
    return

@wamp.register(u'com.poller.rem')
def remove(self, host):
    print 'removing  host : %s ' % host
    self.hostlist.remove(host)
    gevent.kill(self.worker[host])
    self.worker[host].kill()
    self.worker.pop(host, None)
    return
   
def onDisconnect(self):
    print("disconnected")
    reactor.stop()

if name == ‘main’:
from autobahn.twisted.wamp import ApplicationRunner
runner = ApplicationRunner(“ws://127.0.0.1:5010/ws”, “realm1”, debug_app=False, debug_wamp=False)
runner.run(Component)

``

···
0 Likes

#2

i found a wamp v1 library using gevent https://github.com/jgelens/gevent-websocket
wamp V2 is not supported.

Maybe i can use websocket to communicate between wamp application & gevent websocket application?

It is possible to use on the same autobahn application the wamp protocol and websocket ?

i think to do something like that

other wamp app <=> port 5010 wamp app port 5020 <=> gevent websocket application

another problem both gevent & wamp will use the same port 5020.

0 Likes

#3

it’s working!
Could you point my an example using both websocket & wamp on the same autobahn application?

gevent-websocket
#!/usr/bin/env python

from geventwebsocket import WebSocketServer, WebSocketApplication, Resource

class EchoApplication(WebSocketApplication):

def on_open(self):

    print "Connection opened"

def on_message(self, message):

    if message:

        print message 

        self.ws.send(message)   

def on_close(self, reason):

    print 'connection closed'

app = WebSocketServer(

('', 5020),

Resource({'/': EchoApplication})

)

try:

app.serve_forever()

except KeyboardInterrupt:

print "^C detected"

``

autobahn:

#!/usr/bin/env python

from autobahn.twisted.websocket import WebSocketClientProtocol, \

WebSocketClientFactory

class MyClientProtocol(WebSocketClientProtocol):

def onConnect(self, response):

print(“Server connected: {0}”.format(response.peer))

def onOpen(self):

print(“WebSocket connection open.”)

def hello():

self.sendMessage(u"Hello, world!".encode(‘utf8’))

hello()

def onMessage(self, payload, isBinary):

if isBinary:

print(“Binary message received: {0} bytes”.format(len(payload)))

else:

print(“Text message received: {0}”.format(payload.decode(‘utf8’)))

self.sendClose()

def onClose(self, wasClean, code, reason):

print(“WebSocket connection closed: {0}”.format(reason))

if reason:

print(reason)

reactor.stop()

if name == ‘main’:

import sys

from twisted.python import log

from twisted.internet import reactor

log.startLogging(sys.stdout)

factory = WebSocketClientFactory(“ws://localhost:5020”, debug = False)

factory.protocol = MyClientProtocol

reactor.connectTCP(“127.0.0.1”, 5020, factory)

reactor.run()

``

0 Likes