Skip to content

Instantly share code, notes, and snippets.

@sunilmallya
sunilmallya / parent_child_mp_reduction
Last active August 27, 2022 05:03
multiprocessing.reduction socket server with parent processing passing connections to client after accepting connections
#!/usr/bin/env python
"""
@author Sunil Mallya
Sample code to show a parent - child like process communication model where parent listens on a port and passes the pickled file descriptor
to the child process to read the bytes off the socket. The communication in this snippet is via a Queue which is thread/process safe
Just to be clear, the parent process is still accepting the connection and we are sending a live fd to the child
"""
import os
import sys
@sunilmallya
sunilmallya / gist:4671033
Created January 30, 2013 05:55
dumpObject
"""
Dump python object
@author: Not sure, had this code snippet, happy to attribute to who wrote this, saving for personal use
"""
def printDict(di, format="%-25s %s"):
for (key, val) in di.items():
print format % (str(key)+':', val)
def dumpObj(obj, maxlen=77, lindent=24, maxspew=600):
"""Print a nicely formatted overview of an object.
@sunilmallya
sunilmallya / gist:6299539
Created August 21, 2013 20:09
tornado multiprocessing async httpclient
#Pseduo python code
class HttpDownload(object):
def __init__(self,job,ioloop):
self.ioloop = ioloop
#create async http request
self.http_client = tornado.httpclient.AsyncHTTPClient()
self.http_client.fetch(self.req, self.async_callback)
def async_callback(self,reponse):
'''
A Non blocking task scheduler and executor implemented using co routines and Tornado
Torando components acts as a non blocking listener and the 2 co routines scheduler and task exectutor
'''
import tornado.ioloop
import tornado.web
import tornado.httpserver
import tornado.gen
import time
@sunilmallya
sunilmallya / gist:7573627
Last active June 30, 2021 18:23
Mocking Httpclient and AsyncHttpclient in Tornado Server
#!/usr/bin/env python
'''
Mocking Httpclient and AsyncHttpclient in Tornado Server
'''
import unittest
import urllib
import tornado.gen
import tornado.ioloop
import tornado.web
@sunilmallya
sunilmallya / gist:76766675367717cb44f7
Created February 20, 2015 01:30
vim settings for python
syntax on
filetype indent plugin on
setlocal tabstop=4
set tabstop=4 shiftwidth=4 expandtab
set showmatch " highlight matching braces
set comments=sl:/*,mb:\ *,elx:\ */ " intelligent comments
set nocompatible " dont support vi stuff
set autoindent
set smartindent
set textwidth=80
@sunilmallya
sunilmallya / cosinescore
Created March 4, 2015 19:37
Compute cosine score
import math
import stemmer
def irange(sequence):
return zip(range(len(sequence)), sequence)
class CosineScore(object):
def __init__(self,all_docs):
self.documents = all_docs #list all docs [doc1,doc2..]
self.ndocs = len(all_docs)
@sunilmallya
sunilmallya / clousre_compiler
Created March 5, 2015 21:08
Compiling js online with closure
import urllib
import urllib2
CLOSURE_URL = "http://closure-compiler.appspot.com/compile"
def compile_js(contents):
data = { "compilation_level" : "SIMPLE_OPTIMIZATIONS",
"output_format" : "text",
"output_info" : "compiled_code",
"js_code" : contents
@sunilmallya
sunilmallya / redisdbconnection
Created March 5, 2015 23:20
Wrapper class that supports both synchronous and asynchronous redis client
'''
A useful DB Connection class with both sync & async redis clients
It uses a threadpool to make the redis library asynchronous so as
to work with tornado seamlessly. There is also a retry wrapper built in
to retry in case of connection failures to redis server
Tornado 4.0 required, for the rest of the requirements check the imports
'''
<?xml version='1.0' encoding='UTF-8' ?>
<?charles serialisation-version='2.0' ?>
<rewriteSet-array>
<rewriteSet>
<active>true</active>
<name>Rewrite</name>
<hosts>
<locationPatterns>
<locationMatch>
<location>