Joseph
Joseph

Reputation: 3155

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

Upvotes: 17

Views: 11578

Answers (2)

flyer
flyer

Reputation: 9816

I have answered a simliar question and that may solve your problems.
Refer to this question: Do I need call monkey.patch_all() in Django+Gunicorn+GEvent+Boto structure?

First, the gunicorn use SyncWorkers as default and if you don't change the configuration, then the server won't use greenlets. And even though you monkey patch all, I think it doesn't have much help because gunicorn handles one request at a time as default.

Second, have a look at the source code of GeventWorker and it actually monkey patch all.

Upvotes: -2

gwik
gwik

Reputation: 739

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

Upvotes: 10

Related Questions