gevent


How can I test to see whether gevent monkey patching is active?


Is there any "official" way my library can test to see whether gevent monkey patching has been activated?
I don't know about an official way, but here's a way.
Notice os.fork changes when it gets monkey patched:
>>> import os
>>> os.fork
<built-in function fork>
>>> import gevent.monkey; gevent.monkey.patch_all()
>>> os.fork
<function fork at 0x100535140>
And notice what gevent.fork is:
>>> import gevent
>>> gevent.fork
<function fork at 0x100535140>
So, when monkey patching all (or just os):
>>> is_monkeypatched = gevent.fork == os.fork
The short answer is "no" (and, obviously, one would expect to see an API provided in the docs if there were).
The longer answer is "mostly", using monkey.saved. Checking the source you can see that effectively all the usual patching updates this dictionary with references to the original items, so they can be retrieved using monkey.get_original(). This isn't official if you believe officialness requires documentation, but it doesn't seem to be meant as an internal API despite it not being exported in monkey.__all__.
If by your question you mean simply "has any monkey-patching occurred?" then you could just test if monkey.saved: ....
If you mean "has module XXX been monkey-patched?" then you could test for the appropriate name, e.g. if "threading" in monkey.saved: ...
For reference, after doing a monkey.patch_all() here's what you'd see in there. (Note that patch_all() doesn't actually patch everything. It leaves out subprocess, sys, and Event by default, so I included them below.)
>>> import os, gevent
>>> os.name, gevent.__version__
('posix', '1.0.1')
>>> from gevent import monkey
>>> monkey.patch_all(subprocess=True, sys=True, Event=True)
>>> monkey.saved.keys()
['socket', 'thread', 'ssl', 'gevent.socket', 'subprocess', 'sys', 'threading', 'time', 'os', 'select']
See the docs.
from gevent import monkey
monkey.is_module_patched('socket')

Related Links

building gevent in raspberry pi qemu
Some confusion about gevent (cffi) setup
Do rdflib and gevent work nicely together?
Replace gevent.spawn_link_exception
thread exception occurred in gevent
Is join required on gevent gthreads?
ZeroRPC heartbeats and timeouts
NewRelic doesn't graph any data in a Python + Gevent based system
Is there any way to run gevent-socketio 0.3.5-rc2 with gunicorn 18.0 without downgrading
How can I test to see whether gevent monkey patching is active?
How does gevent interact with threading.local data?
werkzeug server failed to handle request
Running time puzzle about gevent
Why does this http streaming not work as expected?

Categories

HOME
asterisk
leaflet
gsm
cxf
gap-system
ebay-api
app-store
snmp
foreach
psexec
collision-detection
markdown
yacc
select2
fastreport
infopath
multibinding
legend-properties
aws-devicefarm
include-path
google-drive-android-api
ps
aws-certificate-manager
knapsack-problem
hierarchy
mobile-development
luhn
rtos
gitosis
powerbi-embedded
tunnel
cocoa-scripting
stochastic-process
libconfig
usernotifications
irr
text-extraction
flask-socketio
serialversionuid
standard-error
dynamic-linking
jenkins-docker
r-commander
lattice
geokit
php-java-bridge
wordpress-plugin
magnet-uri
windows-kernel
gitk
redquerybuilder
persian
bootstrap-tabs
vectordrawable
tidesdk
mergecursor
sony-xperia
method-overloading
information-hiding
jsonschema2pojo
sync
disabled-control
triplestore
yandex-api
iwork
.net-reflector
complete
xml-validation
grunt-wiredep
google-oauth-java-client
friendly-id
fade
objectdatasource
qgraphicsscene
removeall
wordbreaker
system.transactions
boost.build
derived-class
databus
event-propagation
datejs
utm
clgeocoder
windows-xp-embedded
cpack
norton
datarelation
table-valued-parameters
pitch-shifting
versions
web-search
dgml
manuals
software-update
weak-typing

Resources

Encrypt Message