Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why not add the sanic.worker.GunicornWorker to Gunicorn default worker_class #956

Closed
kolapapa opened this issue Sep 28, 2017 · 6 comments
Closed

Comments

@kolapapa
Copy link

kolapapa commented Sep 28, 2017

In my project, what im using is gunicorn and oslo.config , and before i use flask + gunicorn + oslo.config, gunicorn.app.base.BaseApplication This can be written into my code, Why don't you add it to the default?

I tried it:

from sanic.worker import GunicronWorker
class StandaloneApplication(gunicorn.app.base.BaseApplication):

    def __init__(self, app, options=None):
        self.options = {
           'workers': 2,
           'worker_class': GunicronWorker
        }
        self.application = app
        super(StandaloneApplication, self).__init__()
     ...
`
But there's a problem with it.


Traceback (most recent call last):
  File "gunicron_app.py", line 88, in <module>
    StandaloneApplication(app, options).run()
  File "/Users/liukai/workspace/test/sanic_pro/sanic_gunicorn/.venv/lib/python3.5/site-packages/gunicorn/app/base.py", line 203, in run
    super(Application, self).run()
  File "/Users/liukai/workspace/test/sanic_pro/sanic_gunicorn/.venv/lib/python3.5/site-packages/gunicorn/app/base.py", line 72, in run
    Arbiter(self).run()
  File "/Users/liukai/workspace/test/sanic_pro/sanic_gunicorn/.venv/lib/python3.5/site-packages/gunicorn/arbiter.py", line 198, in run
    self.start()
  File "/Users/liukai/workspace/test/sanic_pro/sanic_gunicorn/.venv/lib/python3.5/site-packages/gunicorn/arbiter.py", line 162, in start
    self.log.info("Using worker: %s", self.cfg.worker_class_str)
  File "/Users/liukai/workspace/test/sanic_pro/sanic_gunicorn/.venv/lib/python3.5/site-packages/gunicorn/config.py", line 68, in __getattr__
    raise AttributeError("No configuration setting for: %s" % name)
AttributeError: No configuration setting for: worker_class_str

So I can only change the gunicorn source code.

@jrocketfingers
Copy link
Contributor

Moot point probably, but Gunicron? Not to mention that it expects worker_class_str too, I assume you'd need to expand on the options? Or is that done in the base class init?

@kolapapa
Copy link
Author

kolapapa commented Sep 28, 2017

thank you for your reply. Sry, that's my spelling mistake. the spelling is correct on the code
I also tried to add options, but still did not solve

""" this is gunicron source code"""
class Config(object):

    def __init__(self, usage=None, prog=None):
        self.settings = make_settings()
        self.usage = usage
        self.prog = prog or os.path.basename(sys.argv[0])
        self.env_orig = os.environ.copy()

    def __getattr__(self, name):
        if name not in self.settings:
            raise AttributeError("No configuration setting for: %s" % name)
        return self.settings[name].get()

    def __setattr__(self, name, value):
        if name != "settings" and name in self.settings:
            raise AttributeError("Invalid access!")
        super(Config, self).__setattr__(name, value)
    @property
    def worker_class_str(self):
        uri = self.settings['worker_class'].get()

        ## are we using a threaded worker?
        is_sync = uri.endswith('SyncWorker') or uri == 'sync'
        if is_sync and self.threads > 1:
            return "threads"
        return uri

worker_class_str is from worker_class

def worker_class_str(self):
        uri = self.settings['worker_class'].get()
        return str(uri)          # if i write this, it works
        ...

If you add sanic to worker in gunicorn, it's like the worker of aiohttp, and I'll try to add sanic worker as the addition of aiohttp worker. In Gunicorn's source code, it's really OK

@yunstanford
Copy link
Member

Does it expect string instead ? but i didn't try..
Why do you wanna create the StandaloneApplication class?

cmd line seems to work fine, or provide a config file gunicorn_config.py.

gunicorn --bind 127.0.0.1:1337 --worker-class sanic.worker.GunicornWorker examples.simple_server:app

Maybe i misunderstand your question..

@messense
Copy link
Contributor

The GunicornWorker is intented for using with gunicorn command line.

@messense
Copy link
Contributor

You should avoid changing gunicorn source code IMO, just use the command line or a configuration file.

The gaiohttp gunicorn worker will be deprecated soon.

@kolapapa
Copy link
Author

kolapapa commented Sep 29, 2017

@yunstanford
Because our automated build tools already have one,its like tools/with_venv.sh sanic_web --config-file etc/production/sanic_web.conf .We use oslo.config for unified configuration.These two configurations are not available at the same time.

@messense
emmm... Should I go there proposes a PR? and whats mean of IMO?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants