Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect if Gevent's monkey-patching is active?

Tags:

python

gevent

I have a Python unittest that depends on multiprocessing and therefore must not run when Gevent's monkey-patching is active. Is there a Python statement that can tell me whether gevent.monkey.patch_all has run or not?

like image 733
A. Jesse Jiryu Davis Avatar asked Jul 16 '14 01:07

A. Jesse Jiryu Davis


People also ask

How does monkey patching work?

A monkey patch is a way to change, extend, or modify a library, plugin, or supporting system software locally. This means applying a monkey patch to a 3rd party library will not change the library itself but only the local copy of the library you have on your machine.

What is gevent monkey patching?

Overview. gevent is a coroutine-based cooperative multitasking python framework that relies on monkey patching to make all code cooperative. Gevent actually draws its lineage from Eve Online which was implemented using Stackless Python which eventually evolved into eventlet which inspired gevent.

What does monkey Patch_all () do?

Monkey patching is a technique used to dynamically update the behavior of a piece of code at run-time.

What is monkey patching how do you use it in Python example is it ever a good idea?

In Python, the term monkey patch refers to dynamic (or run-time) modifications of a class or module. In Python, we can actually change the behavior of code at run-time. We use above module (monk) in below code and change behavior of func() at run-time by assigning different value.


3 Answers

I'm not sure there is an idiomatic way, but one simple way would be to check the socket.socket class:

import gevent.monkey, gevent.socket
gevent.monkey.patch_all()
import socket

if socket.socket is gevent.socket.socket:
    print "gevent monkey patch has occurred"
like image 199
Anorov Avatar answered Oct 23 '22 07:10

Anorov


afaik the gevent.monkey.saved dict is only updated when an item is patched, and the original is placed within the dict (and removed on unpatching), e.g.

>>> from gevent.monkey import saved
>>> 'sys' in saved
True
like image 42
Wes Mason Avatar answered Oct 23 '22 09:10

Wes Mason


Here's what I used for detecting if gevent monkey patching is active.

def is_gevent_monkey_patched():
    try:
        from gevent import monkey
    except ImportError:
        return False
    else:
        return bool(monkey.saved)

As A. Jesse Jiryu Davis mentioned, this works for gevent 1.0.x only.

Updated: in gevent 1.1 there's an support API that is helpful to know if objects have been monkey-patched. So the answer for gevent 1.1 could be:

def is_gevent_monkey_patched():
    try:
        from gevent import monkey
    except ImportError:
        return False
    else:
        return monkey.is_module_patched('__builtin__')

BTW, I find that monkey.is_module_patched('sys') always returns False. By looking into monkey.saved.keys() after running monkey.patch_all(), I think only the following modules are valid to check:

['_threading_local', '_gevent_saved_patch_all', 'socket', 'thread', 'ssl', 
 'signal', '__builtin__', 'subprocess', 'threading', 'time', 'os', 'select']
like image 22
Rockallite Avatar answered Oct 23 '22 08:10

Rockallite