从python代码配置uwsgi
uwsgiconf的Python项目详细描述
https://github.com/idlesign/uwsgiconf
说明
从python代码中配置uwsgi
如果你认为你知道你可能错了。总是比你想象的要多。 有太多的子系统和options(800+),甚至很难试图把你的思想。
uwsgiconf允许在python中定义uwsgi配置试图通过以下方式改进:
- 它使用类和方法构造各种子系统的选项;
- 它使用docstring和合理的命名来方便导航;
- 它提供了一些有用的预置来减少样板代码;
- 它鼓励配置重用;
- 它带有cli以便于配置;
- 它具有易于使用和文档化的功能uwsgi stubpython模块;
- 它提供runtime包,类似于uwsgidecorators,但具有更多抽象;
- 它与django框架集成;
- 它能够为systemd、upstart生成配置文件。
考虑将ide与自动完成和docsting支持结合使用,以提高uwsgiconf的效率。
这时您已经知道uwsgiconf只是另一种配置方法。Why?
概述
静态配置
让我们做uwsgicfg.py。在这里,我们使用nicePythonSection预设配置uwsgi来运行我们的web应用程序。
fromuwsgiconf.configimportconfigure_uwsgifromuwsgiconf.presets.niceimportPythonSectiondefget_configurations():"""This should return one or more Section or Configuration objects. In such a way you can configure more than one uWSGI instance in the same place. Here we'll define just one configuration section, which will instruct uWSGI to serve WSGI application (from wsgi.py module) on http://127.0.0.1:8000 """section=PythonSection(wsgi_module='/home/idle/myapp/wsgi.py',).networking.register_socket(PythonSection.networking.sockets.http('127.0.0.1:8000'))returnsection# Almost done. One more thing:configure_uwsgi(get_configurations)
现在,如果要生成myconf.ini文件并手动将其用于uwsgi,可以使用:
$ uwsgiconf compile > myconf.ini $ uwsgi myconf.ini
或者使用uwsgiconf为模块中定义的配置自动生成uwsgi进程:
$ uwsgiconf run
注意:uwsgiconfcli需要clickpackage可用(可以与uwsgiconf一起安装)。
运行时配置
uwsgiconf附带了runtimepackage,它类似于uwsgidecorators但提供了不同的抽象。
当实际的抽象不可用时,这些抽象也将使用存根uwsgi模块。
几个例子:
fromuwsgiconf.runtime.lockingimportlockfromuwsgiconf.runtime.schedulingimportregister_timer_rb@register_timer_rb(10,repeat=2)defrepeat_twice():"""This function will be called twice with 10 seconds interval (by default in first available mule) using red-black tree based timer. """withlock():# Code under this context manager will be locked# using default (0) uWSGI lock.do_something()
第三方
django.使用manage命令在uwsgi上运行基于django的项目:
$ ./manage.py uwsgi_run $ ./manage.py uwsgi_reload --force
其他命令可用。
系统配置
编译系统服务配置(例如systemd)以运行uwsgi支持的项目:
$ uwsgiconf sysinit systemd