• unlike process, threads do not have their own PCB • runs within a process • shares code, data, heap • can be concurrent and parallel (warning: GIL limitation in python) Saturday, June 15, 13
executed • runs within the kernel • shares code, data, heap, stack • holds information about active processes in data structures called PCB • can be concurrent and parallel Saturday, June 15, 13
gevent • “lightweight threads”- different from POSIX threads (pthreads) - no implicit scheduling • we specify when the code runs or when it gets switched away Saturday, June 15, 13
• written in C • When a socket is created, a communication endpoint becomes available • A file descriptor is returned • “Everything is a file” Saturday, June 15, 13
sockets) • written in C • When a socket is created, a communication endpoint becomes available • A file descriptor is returned • “Everything is a file” • Various languages provide similar interfaces and are essentially wrappers Saturday, June 15, 13
HTTP • “Upgrade” request in HTTP 1.1 • Server can also force an upgrade by sending “426 upgrade required” • stream of messages, not bytes, splitted across data frames, end marked by FIN • connection persists Saturday, June 15, 13
• 6 transports ✓WebSocket ✓Adobe Flash Socket ✓AJAX long polling ✓AJAX multipart streaming ✓Forever IFrame ✓JSONP polling • Most capable transport selected at runtime no change required to its abstracted API! socket.io-transports analogous to orm-(db adaptors and databases) Saturday, June 15, 13
original (nodejs) javascript implementation ✓python ✓perl ✓java ✓golang ✓erlang • why? Because fundamentally, Socket.IO is logic built on top of sockets. And as we already know, various languages have their own interfaces to Berkeley sockets! Saturday, June 15, 13
socket = io.connect("/chat"); // .. other js logic </script> • flash transport (WEB_SOCKET_SWF_LOCATION) optional • install socket.io.js client (which comes with WebSocketMain.swf which we can use optionally) via nodeenv, npm and bower • establish a “socket” js object via io.connect() • “/chat” is a Socket.IO namespace which is like a “controller” and NOT a url (“router”) (client-side “Controllers”) Saturday, June 15, 13
monkey.patch_all() PORT = 5000 if __name__ == '__main__': print 'Listening on http://127.0.0.1:%s and on port 10843 (flash policy server)' % PORT SocketIOServer(('', PORT), app, resource="socket.io").serve_forever() Saturday, June 15, 13
WEB_SOCKET_SWF_LOCATION = '/static/js/socketio/WebSocketMain.swf', socket = io.connect('/chat'); socket.on('connect', function () { $('#chat').addClass('connected'); socket.emit('join', window.room); }); socket.on('announcement', function (msg) { $('#lines').append($('<p>').append($('<em>').text(msg))); }); socket.on('nicknames', function (nicknames) { $('#nicknames').empty().append($('<span>Online: </span>')); for (var i in nicknames) { $('#nicknames').append($('<b>').text(nicknames[i])); } }); socket.on('msg_to_room', message); socket.on('reconnect', function () { $('#lines').remove(); message('System', 'Reconnected to the server'); }); socket.on('reconnecting', function () { message('System', 'Attempting to re-‐connect to the server'); }); (client-side “Controllers”) Saturday, June 15, 13