Skip to content

Instantly share code, notes, and snippets.

@jtallieu
jtallieu / pickleSocks.py
Created October 5, 2019 19:47
Passing Foo instance over a socket.
import gevent
from gevent import monkey
monkey.patch_all()
import os
os.environ['MONKEY'] = "True"
import pickle
import socket
r, w = socket.socketpair()
@jtallieu
jtallieu / picklePiper.py
Created October 5, 2019 19:46
Passing python object over a Pipe
import gevent
from gevent import monkey
monkey.patch_all()
import os
os.environ['MONKEY'] = "True"
import io
from multiprocessing import Pipe
from gevent.socket import wait_read, wait_write
@jtallieu
jtallieu / oxlock.py
Created February 12, 2019 07:23
Weird variation of Read-Write Lock using gevent. One function, on being called must lock out any other related function for a specific time.
"""
Imagine a situation where you have two functions
that operate on the same hashable resource (it has an ID).
And that those two functions can be called from various
threads that you have no control over. Each of these functions
can run multiple times and concurrently. Once one of the
functions for a resource is called, neither of the functions
can be allowed to run.
In specific terms, I have post_save and post_delete Django
@jtallieu
jtallieu / toggle_analytics.py
Created February 25, 2018 02:50
Toggle Eagle Eye Networks analytics on an esn from a master account user using the API
"""
This script till toggle the analytics feature on an esn in a tenant account.
To disable the analytics, you must delete the 'active_application' settings in
the user settings of the camera. In order to re-enable the feature, you must
add the settings back to the 'active_settings'. To accomplish this, we must
store the settings somewhere in the camera settings so that when we want to
enable we can copy the settings from somewhere. This script uses
'inactive_application' to hold the settings for us. You may choose another
name if you like.
@jtallieu
jtallieu / gevent_yield.py
Created September 8, 2017 13:55
Backing a Python generator with a pool of gevent greenlets that provide async data
"""
Gevent example to illustrate using AsyncResults from a greenlet
I wanted to launch a pool or workers that would generate data
that is provided through an iterator.
"""
import random
import time
import gevent
@jtallieu
jtallieu / DictFilter.py
Created August 29, 2017 13:21
Django like filters for dictionaries
import operator
class Filter(object):
"""
Filter class to filter on properties of a dict.
Arguments: property, op, value
Will type the existing value of the dictionary
@jtallieu
jtallieu / GroupedTagMap.py
Created August 13, 2017 15:10
Dict like data structure for operating on tag results from Clarifai - where a list of tags and scores are grouped by models.
class TagMap(dict):
"""
Interpret tag values {<model>:{name: value}, ..} and wrap to get '.' access to the value.
data = {'general': [('joey', 10), ('x y', 22)], 'een': ('ken', 30)]}
tm = TagMap(data)
print tm.joey
print tm['x y']
print tm.x_y
@jtallieu
jtallieu / DotDict.py
Last active August 13, 2017 15:01
Yet another iteration at providing 'dot' access to dict items. DOES NOT COPY with copy.copy()
class Mapping(dict):
"""
Example: Map({'make': 'Toyota'}, model='Tacoma', year=1998, color='white')
"""
def __init__(self, *args, **kwargs):
super(Mapping, self).__init__(*args, **kwargs)
for arg in args:
if isinstance(arg, dict):
for k, v in arg.iteritems():
self[k] = v
@jtallieu
jtallieu / plotter.py
Created March 5, 2017 02:48
Plotter library function to graph Gevent benchmarks with Plotly
import plotly.plotly as py
from plotly import tools
from plotly.graph_objs import *
from plotly.offline import plot
from numpy import *
# Make a gradient pattern of 12 colors.
N = 12
colors = ['hsl('+str(h)+',50%'+',50%)' for h in linspace(0, 360, N)]
@jtallieu
jtallieu / bnch_scheduling.py
Last active March 5, 2017 02:42
Gevent scheduling script to determine scheduling algorithm
import sys
import gevent
import timeit
from gevent.event import Event
from plotter import graph_latency, graph_ops
RUNS = [100, 500, 1000, 5000, 10000, 20000, 30000, 100000]
SLEEP_TIME = float(sys.argv[1])
LOAD_TIME = 3