Source

MyIOTest / myiotestb / views.py

The default branch has multiple heads

Full commit
from myiotestb.models import DBSession
from myiotestb.models import MyModel
from pyramid.view import view_config
from pyramid_socketio.io import SocketIOContext, socketio_manage
from pyramid.response import Response

import gevent

#class ConnectIOContext(SocketIOContext):
#    # self.io is the Socket.IO socket
#    # self.request is the request
#    def msg_connect(self, msg):
#        print "Connect message received", msg
#        self.msg("connected", hello="world")

class ConnectIOContext(SocketIOContext):
    def msg_connect(self, msg):
        def sendcpu():
            """Calculate CPU utilization"""
            prev = None
            while self.io.connected():
                vals = map(int, [x for x in open('/proc/stat').readlines()
                                 if x.startswith('cpu ')][0].split()[1:5])
                if prev:
                    percent = (100.0 * (sum(vals[:3]) - sum(prev[:3])) / 
                               (sum(vals) - sum(prev)))
                    self.msg("showdata", point=percent)
                prev = vals
                gevent.sleep(0.5)
        self.spawn(sendcpu)

# Socket.IO implementation
@view_config(route_name="socket_io")
def socketio_service(request):
    print "call de la vue socketio_service"
    retval = socketio_manage(ConnectIOContext(request))
    return Response(retval)

@view_config(route_name='home', renderer='myiotestb:templates/index.mako')
def test(request):
    """
    """
    print "socket io is inside request.environ"
    print request.environ['socketio']
    return {}