Skip to content

Instantly share code, notes, and snippets.

@jimjh
jimjh / jq-select-by-length.sh-session
Created Jun 18, 2014
jq - select objects based on lengths of arrays
View jq-select-by-length.sh-session
# objects with at least one widget and at least one user
$ cat part-r-00000 | jq 'select((.widgets | length) > 0 and (.users | length) > 0)'
@jimjh
jimjh / github.rb
Created Jul 12, 2013
Caching with github_api, faraday-http-cache, and moneta
View github.rb
#!/usr/bin/env ruby
# Usage:
# load './github.rb'
# 10.times { fire }
require 'github_api'
require 'faraday-http-cache'
require 'moneta'
require 'active_support/cache/moneta_store'
require 'logger'
@jimjh
jimjh / gevent_tracer.py
Last active Jun 22, 2020
gevent_tracer.py
View gevent_tracer.py
def _callback(event, args):
# switch and throw both switch the active greenlet from origin to target
if event not in set(['switch', 'throw']):
return
origin, target = args
# record last switch (time and CPU tick)
global __last_switch_time_ms, __last_switch_cpu_tick
@jimjh
jimjh / pymysql-profile.sh-session
Last active Feb 23, 2020
Measuring the amount of memory used by PyMySQL
View pymysql-profile.sh-session
$ python -m memory_profiler mysql.py
Filename: mysql.py
Line # Mem usage Increment Line Contents
================================================
4 19.375 MiB 19.375 MiB @profile
...
7 19.742 MiB 0.367 MiB connection = pymysql.connect(...)
...
13 19.742 MiB 0.000 MiB connection.close()
View event-proxy-buffer-stacktrace.gdb
(gdb) bt
#0 0x0000555555555f3b in readcb ()
#1 0x00007ffff7983e32 in ?? () from /usr/lib/x86_64-linux-gnu/libevent-2.1.so.6
#2 0x00007ffff79898f8 in ?? () from /usr/lib/x86_64-linux-gnu/libevent-2.1.so.6
#3 0x00007ffff798a33f in event_base_loop () from /usr/lib/x86_64-linux-gnu/libevent-2.1.so.6
#4 0x00005555555568ff in _init_event_loop ()
#5 0x0000555555556527 in proxy ()
#6 0x00005555555563f4 in main ()
@jimjh
jimjh / event-proxy-stacktrace.gdb
Created Feb 23, 2020
Stacktrace of event-proxy in GDB.
View event-proxy-stacktrace.gdb
(gdb) bt
#0 0x00005555555559aa in do_accept ()
#1 0x00007ffff79898f8 in ?? () from /usr/lib/x86_64-linux-gnu/libevent-2.1.so.6
#2 0x00007ffff798a33f in event_base_loop () from /usr/lib/x86_64-linux-gnu/libevent-2.1.so.6
#3 0x000055555555697a in _init_event_loop ()
#4 0x00005555555565a2 in proxy ()
#5 0x000055555555646f in main ()
@jimjh
jimjh / gevent_file_io.py
Last active Nov 9, 2019
TIL that gevent does not switch when you write to a file, even with monkey patching
View gevent_file_io.py
#! encoding: utf-8
"""Apparently gevent doesn't switch when writing to files on disk."""
from __future__ import print_function
import gevent
from gevent import monkey
from gevent.fileobject import FileObject
monkey.patch_all()
import socket
import tempfile
View compare_patch
$ python late_patch.py
sleep(seconds)
Delay execution for a given number of seconds. The argument may be
a floating point number for subsecond precision.
$ python eager_patch.py
Put the current greenlet to sleep for at least *seconds*.
*seconds* may be specified as an integer, or a float if fractional
View eager_patch.py
from __future__ import print_function
from gevent.monkey import patch_all
patch_all()
from time import sleep
print(sleep.__doc__)
View late_patch.py
from __future__ import print_function
from time import sleep
from gevent.monkey import patch_all
patch_all()
print(sleep.__doc__)