Skip to content

Instantly share code, notes, and snippets.

@mrjoes
mrjoes / gist:1462715
Created December 11, 2011 21:00
Dummy websocket client
import websocket
import threading
import random
import time
import sys
class Worker(threading.Thread):
def __init__(self, *args, **kwargs):
self.lock = threading.RLock()
self.packets_sent = 0
@mrjoes
mrjoes / gist:1462720
Created December 11, 2011 21:01
Dummy server
from tornado import web, websocket, ioloop
class Test(websocket.WebSocketHandler):
clients = set()
def open(self):
self.clients.add(self)
def on_message(self, msg):
for c in self.clients:
sent: number of messages sent by one client per second
recv: total number of messages received per one second
pings: in milliseconds
node.js echo server with commented log output:
clients: 5, sent: 8759.521709, recv: 43795.308466, min_ping: 0, max_ping: 4, avg_ping: 0.399996, errors: 0
clients: 25, sent: 1998.742675, recv: 49938.586699, min_ping: 0, max_ping: 20, avg_ping: 1.998743, errors: 0
clients: 50, sent: 1051.625850, recv: 52363.271976, min_ping: 0, max_ping: 59, avg_ping: 5.885065, errors: 0
clients: 100, sent: 514.820466, recv: 50273.736734, min_ping: 0, max_ping: 233, avg_ping: 22.884596, errors: 0
clients: 150, sent: 365.081970, recv: 52460.994307, min_ping: 0, max_ping: 473, avg_ping: 46.202689, errors: 0
First version:
def make_line_iter(iter):
buf = ''
last_pos = 0
for p in iter:
buf += p
lb = buf.find('\n', last_pos)
import Queue
import threading
import functools
import traceback
from tornado.ioloop import IOLoop
THREAD_LIMIT = 8
_task_queue = Queue.Queue(THREAD_LIMIT * 2)
# With a callback
def test():
def handle_user(username, first_name, last_name, exception=None):
if exception:
# Blow up
return
print username, first_name, last_name
get_user_id(10, handle_user)
Error thrown at line 667, column 4 in <anonymous function: XHRObject.prototype._start>(method, url, payload) in http://cdn.sockjs.org/sockjs-0.2.1.js:
that.xhr.send(payload);
called from line 615, column 43 in <anonymous function: utils.XHRObject>() in http://cdn.sockjs.org/sockjs-0.2.1.js:
that._start(method, url, payload);
Uncaught exception: ReferenceError: Security violation
Error thrown at line 667, column 4 in <anonymous function: XHRObject.prototype._start>(method, url, payload) in http://cdn.sockjs.org/sockjs-0.2.1.js:
that.xhr.send(payload);
called from line 615, column 43 in <anonymous function: utils.XHRObject>() in http://cdn.sockjs.org/sockjs-0.2.1.js:
that._start(method, url, payload);
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script src="http://cdn.sockjs.org/sockjs-0.1.2.min.js"></script>
<script>
$(function() {
var conn = null;
function log(msg) {
# -*- coding: utf-8 -*-
"""
Simple sockjs-tornado chat application. By default will listen on port 8080.
"""
import tornado.ioloop
import tornado.web
import sockjs.tornado
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
# Simple database and session wrapper. I use it in my Flask app too.
class Database(object):
def __init__(self):
self.session_maker = sessionmaker()
self.session = scoped_session(self.session_maker)
self.engine = None