16

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

Joseph
  • 3,085
  • 3
  • 23
  • 33

2 Answers2

9

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
gwik
  • 679
  • 5
  • 9
  • Hi, thanks for the tip, it calls patch_all() which by default does not patch urllib, so how can i patch it at initialization? and is there a way to determined what is patched and what isn't? thanks – Joseph Aug 18 '12 at 04:30
  • Not really but subsequent monkey patching is ok. – gwik Aug 18 '12 at 16:05
  • ok, some off-topic question: why gunicorn paster? what is its benefits? thanks – Joseph Aug 18 '12 at 20:16
  • 1
    Well, taste it is, also a lot of server are using PasteDeploy lately. – gwik Aug 19 '12 at 00:59
0

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.

Community
  • 1
  • 1
flyer
  • 9,280
  • 11
  • 46
  • 62
  • 3
    He says above in the first sentence that he is using the gevent worker type and then you just repeated exactly what the first answer said. – dave4jr Jun 25 '18 at 07:52