import gevent.monkey
gevent.monkey.patch_all()
import psycogreen.gevent
psycogreen.gevent.patch_psycopg()
# Log how much time each greenlet runs before it yields control back to the gevent loop.
#
# This can help diagnose issues where it appears that some greenlets are running for too
# long before yielding, and thus causing other greenlets to starve.
#
# More info:
if environ.get('LOG_GREENLET_RUN_DURATIONS') and environ.get('LOG_GREENLET_RUN_DURATIONS').lower() == 'true':
import time, greenlet, gevent.hub, threading
MIN_DURATION_TO_LOG = float(environ.get('MIN_GREENLET_RUN_DURATION_TO_LOG', 0.5)) # seconds
def log_greenlet_run_duration(what, (origin, target)):
global _last_switch_time
then = _last_switch_time
now = _last_switch_time = time.time()
if then is not None:
blocking_time = now - then
if origin is not gevent.hub.get_hub() and blocking_time > MIN_DURATION_TO_LOG:
msg = "Greenlet ran for %.4f seconds (%s from %s %s to %s %s).\n"
msg = msg % (blocking_time, what, type(origin), id(origin), type(target), id(target))
print msg
greenlet.settrace(log_greenlet_run_duration)