Create your own awesome maps

Even on the go

with our free apps for iPhone, iPad and Android

Get Started

Already have an account?
Log In

Doing comet in Python using gevent, Uriel Katz by Mind Map: Doing comet in Python using
gevent, Uriel Katz
0.0 stars - reviews range from 0 to 5

Doing comet in Python using gevent, Uriel Katz

easy to use, co-routine-based networking library for python

abstract is, & lets you write regular threading code


loads the server, network, &c


CTO @ BinFire


Open-source COMET server released by Uriel

Non blocking servers

at the heart of every non-blocking networking framework

select(read_fds, write_fds, error_fds)

most are single-threaded

event handling must be as fast as possible

using single threads leads to low memory consumption

there are 2 common ways to do it

using callbacks, Twisted, Node.js

co-routines, gevent


aka green-threads, greenlets

Similar to kernel level threads

much more lightweight

Kind of userland cooperative multi-tasking

they each yield to let others to run as well

each has its own stack in the heap


the co-routine actually does the select call

or poll/epoll

has a really fast wsgi server

must be very careful not to block

you kill the server

monkey-patches python

every IO call is safe for freenlets

Pushing data to the client

When web clients need real-time updates

We need a way to push information, without it being requsted

But HTTP only supports request model

few ways

long-polling hack, request doesn't return until there's an answer, The problem is the memory consumption of all open threads, e.g., Apache

websockets, sort of TCP protocol, not yet supported in browsers

Architecture of Shaveet

Based on gevent wsgi server

which in turn is based on libevent

Messages & clients are stored in-memory

Exposes API in JSON-RPC

2 kinds of transports, JSONP, CORS, AJAX across-domains

No authentication betweeb webapp & Shaveet, only the webapp should access Shaveet JSON-RPC API

Each client has a secret key used for getting notifications from Shaveet

built around a pub/sub paradigm

channels, opened by the webapp, best metaphore is radio stations, you only get messages for the station you listen to, while you're listening, See Nicholas Piel, see also his current startup SiteSupport

in each chennel, loop on connected clients & send notifications

Taking out the trash - GC

Clients can come & go, & Shaveet needs to be aware of it

Goes over all clients periodically

kills clients who's last poll was more than 25s ago

blocks the server for <1ms


a Python WSGI protocol, exposing API

create_client ( client_id )

returns the secret key

subscribe ( client_id, channel_name )

creates the channel, if there isn't one


new_message ( client_id, message, channel_name )

get_channel_clients ( channel_name )

get_client_channels ( client_id )




import Shaveet.js

static file served by Shaveet


Shaveet.init ( client_id, key )

Shaveet.listenSingle ( channel_name, callback )


Shaveet.stopListening ( channel_name )

You send messages via the webapp, not Shaveet

e.g., regular AJAX