0

preface: I would like to separate these problems into smaller questions, but apparently, I am missing some pieces of the puzzle and it seems impossible to me.

I developed my cherrypy application using cherrypy's built in WSGI server. I naively assumed that when the time comes, I will be able to use created WSGI Application class and deploy it using any WSGI compliant server.

I used this blog post to create my own (but very similar) cherrypy Plugin and Tool to connect to database using SQLAlchemy during http requests.

I expected that any server will somehow work like cherrypy's built in server:

  1. main process will spawn X threads to satisfy X concurrent requests
  2. my engine Plugin will create SQLalchemy engine with connection pool = X (so any request will have its connection)
  3. on request arrival, my Tool will supply sql alchemy connection from pool

This flow does not match with uWSGI (as long as I understand it).

I assign my application.py in uWSGI configuration. This file looks something like this:

cherrypy.tools.db = DbConnectorTool()
cherrypy.engine.dbengine = DbEnginePlugin(cherrypy.engine, settings.database)
cherrypy.config.update({
    'engine.dbengine.on': True
})

from myapp.application import Application
root = Application(settings)
application = cherrypy.Application(root, script_name='', config=settings)

I was using this application.py to mount my application into cherrypy's built in server when I was developing and testing it.

The problems are that uWSGI does not create any threads itself and my SQLAlchemy plugin is not working with it, because no cherrypy.engine is created.

Does uWSGI support threading in the meaning of using threads to serve multiple concurrent requests? Can I start these threads in my application.py? Will uWSGI understand it and use these threads for concurrent requests? And how can this be done? I think cherrypy can be used somehow, or not? And what about my SQLAlchemy Plugin, how can I start cherrypy.engine when using only WSGI cherrypy.Application?

Any help or information that could help me will be appreciated.

Edit:

My uWSGI configuration:

<uwsgi>
    <socket>127.0.0.1:9001</socket>
    <master/>
    <daemonize>/var/log/uwsgi/app.log</daemonize>
    <logdate/>
    <threads/>
    <pidfile>/home/web/uwsgi.pid</pidfile>
    <uid>uwsgi</uid>
    <gid>uwsgi</gid>
    <workers>2</workers>
    <harakiri>90</harakiri>
    <harakiri-verbose/>
    <home>/home/web/</home>
    <pythonpath>/home/web/instance</pythonpath>
    <module>core.application</module>
    <no-orphans/>
    <touch-reload>/home/web/uwsgi-reload-web</touch-reload>
</uwsgi>
4

2 回答 2

6

uWSGI 使用工作进程,而不是线程。值得注意的是,这意味着不再在所有请求之间共享全局变量。您可以将SharedArea用于全局数据。默认情况下,这些进程是分叉的,因此请确保您对此感到满意或调整设置(请参阅要知道的事情)。

通过调用获取 Cherrypy 的 WSGI 应用程序cherrypy.tree.mount(root, config=settings)

如果您的数据库插件没有线程/共享数据问题,那么它很可能会起作用。就像你说的,你可能需要cherrypy.engine.start(),但绝对不需要cherrypy.engine.block(),因为你的主线程现在是 uWSGI worker。

于 2012-12-18T21:36:21.657 回答
0

您应该发布您的 uWSGI 配置,否则将很难理解发生了什么。

顺便说一下产生额外的线程(每个工人)你只需要添加 --threads N

于 2012-12-09T07:15:12.283 回答