Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which features are monkey patched by gunicorn gevent worker?

I am using gunicorn to run some flask and django applications, using the gevent worker, and have some questions...

First, I assume that because gunicorn fork and instantiate my processes, it will monkey patch the standard modules, and i do not have to call monkey.patch_all myself, it's already done for me, and each request is running as a greenlet, Is that correct?

Second, and this is the important part, which featues are really got monkey patched by gunicorn-gevent? when you use gevent, you can always choose which feature to patch(socket, patch, urllib)... So, the question is , Which of these featured are really got monkey patched bu gunicorn-gevent? How can i change this list?

For example, the standard call to monkey.patch_all() does not patch urllib? How could i know if it was patched or not? and how to force gunicorn-gevent to patch it?

Thanks

Joe

like image 928
Joseph Avatar asked Aug 17 '12 04:08

Joseph


1 Answers

Looks like the gevent worker calls monkey.patch_all() when it is initialized.

https://github.com/benoitc/gunicorn/blob/master/gunicorn/workers/ggevent.py#L45

You can still call your own initialization code when your app boots.

With flask I use gunicorn paster.

my_app.ini:

[app:main]
use = egg:mypackage#myapp
# app config goes here

[server:main]
use = egg:gunicorn#main
# you can put gunicorn config options here

setup.py in your package:

entry_points={
    'paste.app_factory': [
        'myapp = mypackage.module:app_factory'
    ]

example mypackage/module.py:

def app_factory(global_config, **config):
    # initialization code / gevent monkey patch goes here
    # also you can assemble your wsgi stack.
    # then return your flask app
    return app

Now you can run it:

gunicorn_paster my_app.ini
like image 135
gwik Avatar answered Oct 01 '22 16:10

gwik