Skip to content

Instantly share code, notes, and snippets.

View ssadler's full-sized avatar
🤍

Scott Sadler ssadler

🤍
  • utxocontracts.com
  • London, UK
View GitHub Profile
import tornado.web
from multiprocessing import Pipe
from _multiprocessing import Connection
class MultiprocessApplication(tornado.web.Application):
def __init__(self, *args, **kwargs):
self._conn_main_recv, self._conn_main_send = Pipe()
tornado.web.Application.__init__(self, *args, **kwargs)
def setup(self, ioloop, is_main_process):
import tornado.web
from multiprocessing import Pipe
from _multiprocessing import Connection
class MultiprocessApplication(tornado.web.Application):
def __init__(self, *args, **kwargs):
self._conn_main_recv, self._conn_main_send = Pipe()
tornado.web.Application.__init__(self, *args, **kwargs)
def setup(self, ioloop, is_main_process):
import os
import tornado.httpserver
import tornado.ioloop
import tornado.web
from multiprocessing import Pipe
class HelloHandler(tornado.web.RequestHandler):
def get(self):
self.write(self.application.communicate('hello world'))
> python -m cProfile -s time client.py
3806027 function calls (3805761 primitive calls) in 39.336 CPU seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
100000 21.909 0.000 37.780 0.000 {method 'recv_multipart' of 'zmq._zmq.Socket' objects}
900002 8.073 0.000 8.920 0.000 threading.py:179(__init__)
900002 3.256 0.000 12.176 0.000 threading.py:174(Condition)
300000 3.061 0.000 15.871 0.000 Queue.py:22(__init__)
@ssadler
ssadler / zmq_recv_bench.py
Created November 8, 2010 23:29
code and results for different ways of doing recv() in pyzmq
import sys
import os, os.path
sys.path.insert(0, os.getcwd())
import zmq
addr = 'inproc://tmp1'
msg = '0'
copy = True
sw = zmq.Stopwatch()
cycles = int(sys.argv[1])
@ssadler
ssadler / tornado_passfd_demo.py
Created March 20, 2011 05:08
Cycle a tornado server passing server fd (buggy)
import os, sys, socket, passfd
import tornado.httpserver
import tornado.ioloop
import tornado.web
import time
class HelloHandler(tornado.web.RequestHandler):
def get(self):
self.write('hello from pid: %s' % os.getpid())
class What(object):
def __init__(self, arg={}):
self.arg = arg
assert not len(self.arg)
arg[1] = 2
What()
# Whoops... arg is a mutable object
@ssadler
ssadler / shit_json.py
Created March 2, 2013 12:19
json parser
def parse(data):
char = data.pop(0)
if char == '[':
return parse_list(data)
elif char == '{':
return parse_object(data)
elif '0' <= char <= '9':
return parse_int(char, data)
elif char in 'tfn':
@ssadler
ssadler / httpexpression.py
Last active December 20, 2016 17:00
http expression
#!/usr/bin/env python
main = lambda view, port, host='0.0.0.0', recvlen=4096, recvtimeout=0.0: (
(lambda sys, socket, itertools, urllib, re, cStringIO, pprint, datetime, types, select, traceback, BaseHTTPServer:
(lambda _try, recursive:
(lambda HttpRequest, HttpResponse:
(lambda conn2buf, serverloop, log_hit, errorview:
(lambda sock, handler:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) or
import Data.List (partition)
sortByOther :: String -> String -> String
sortByOther [] _ = []
sortByOther (x:xs) ins = matches ++ sortByOther xs rejects
where (matches, rejects) = partition (==x) ins
main = putStrLn $ sortByOther "abcd" "ccbbaae"