Skip to content

Instantly share code, notes, and snippets.

@jimjh
jimjh / jq-select-by-length.sh-session
Created June 18, 2014 16:24
jq - select objects based on lengths of arrays
# 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 / gevent_tracer.py
Last active June 23, 2023 01:49
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 / github.rb
Created July 12, 2013 15:05
Caching with github_api, faraday-http-cache, and moneta
#!/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 / pymysql-profile.sh-session
Last active February 23, 2020 20:52
Measuring the amount of memory used by PyMySQL
$ 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()
(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 February 23, 2020 17:19
Stacktrace of event-proxy in 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 November 9, 2019 08:01
TIL that gevent does not switch when you write to a file, even with monkey patching
#! 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
$ 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
from __future__ import print_function
from gevent.monkey import patch_all
patch_all()
from time import sleep
print(sleep.__doc__)
from __future__ import print_function
from time import sleep
from gevent.monkey import patch_all
patch_all()
print(sleep.__doc__)