Refactor gevent interaction