Permalink
Browse files

add Sphinx documentation, closes gh-3

  • Loading branch information...
1 parent 6fcfbfd commit a9d872736aa2b69c5a88881714fa8341056335f8 @eklitzke eklitzke committed Jun 25, 2011
Showing with 425 additions and 69 deletions.
  1. +1 −0 .gitignore
  2. +4 −1 Makefile
  3. +1 −0 README.rst
  4. +130 −0 docs/Makefile
  5. +216 −0 docs/source/conf.py
  6. +73 −68 README.md → docs/source/index.rst
View
1 .gitignore
@@ -3,3 +3,4 @@
current
tmp_*
zygote.egg-info
+docs/build
View
5 Makefile
@@ -1,4 +1,4 @@
-.PHONY: pyflakes clean test production
+.PHONY: pyflakes clean test production docs
pyflakes:
find . -name '*.py' -print0 | xargs -0 pyflakes
@@ -14,3 +14,6 @@ serviceinit.d:
mkdir serviceinit.d
production: serviceinit.d
+
+docs:
+ make -C docs html
View
1 README.rst
View
130 docs/Makefile
@@ -0,0 +1,130 @@
+# Makefile for Sphinx documentation
+#
+
+# You can set these variables from the command line.
+SPHINXOPTS =
+SPHINXBUILD = sphinx-build
+PAPER =
+BUILDDIR = build
+
+# Internal variables.
+PAPEROPT_a4 = -D latex_paper_size=a4
+PAPEROPT_letter = -D latex_paper_size=letter
+ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source
+
+.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest
+
+help:
+ @echo "Please use \`make <target>' where <target> is one of"
+ @echo " html to make standalone HTML files"
+ @echo " dirhtml to make HTML files named index.html in directories"
+ @echo " singlehtml to make a single large HTML file"
+ @echo " pickle to make pickle files"
+ @echo " json to make JSON files"
+ @echo " htmlhelp to make HTML files and a HTML help project"
+ @echo " qthelp to make HTML files and a qthelp project"
+ @echo " devhelp to make HTML files and a Devhelp project"
+ @echo " epub to make an epub"
+ @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter"
+ @echo " latexpdf to make LaTeX files and run them through pdflatex"
+ @echo " text to make text files"
+ @echo " man to make manual pages"
+ @echo " changes to make an overview of all changed/added/deprecated items"
+ @echo " linkcheck to check all external links for integrity"
+ @echo " doctest to run all doctests embedded in the documentation (if enabled)"
+
+clean:
+ -rm -rf $(BUILDDIR)/*
+
+html:
+ $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
+ @echo
+ @echo "Build finished. The HTML pages are in $(BUILDDIR)/html."
+
+dirhtml:
+ $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml
+ @echo
+ @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml."
+
+singlehtml:
+ $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml
+ @echo
+ @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml."
+
+pickle:
+ $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle
+ @echo
+ @echo "Build finished; now you can process the pickle files."
+
+json:
+ $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json
+ @echo
+ @echo "Build finished; now you can process the JSON files."
+
+htmlhelp:
+ $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp
+ @echo
+ @echo "Build finished; now you can run HTML Help Workshop with the" \
+ ".hhp project file in $(BUILDDIR)/htmlhelp."
+
+qthelp:
+ $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp
+ @echo
+ @echo "Build finished; now you can run "qcollectiongenerator" with the" \
+ ".qhcp project file in $(BUILDDIR)/qthelp, like this:"
+ @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/Zygote.qhcp"
+ @echo "To view the help file:"
+ @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/Zygote.qhc"
+
+devhelp:
+ $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp
+ @echo
+ @echo "Build finished."
+ @echo "To view the help file:"
+ @echo "# mkdir -p $$HOME/.local/share/devhelp/Zygote"
+ @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/Zygote"
+ @echo "# devhelp"
+
+epub:
+ $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub
+ @echo
+ @echo "Build finished. The epub file is in $(BUILDDIR)/epub."
+
+latex:
+ $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
+ @echo
+ @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex."
+ @echo "Run \`make' in that directory to run these through (pdf)latex" \
+ "(use \`make latexpdf' here to do that automatically)."
+
+latexpdf:
+ $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
+ @echo "Running LaTeX files through pdflatex..."
+ make -C $(BUILDDIR)/latex all-pdf
+ @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."
+
+text:
+ $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text
+ @echo
+ @echo "Build finished. The text files are in $(BUILDDIR)/text."
+
+man:
+ $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man
+ @echo
+ @echo "Build finished. The manual pages are in $(BUILDDIR)/man."
+
+changes:
+ $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes
+ @echo
+ @echo "The overview file is in $(BUILDDIR)/changes."
+
+linkcheck:
+ $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck
+ @echo
+ @echo "Link check complete; look for any errors in the above output " \
+ "or in $(BUILDDIR)/linkcheck/output.txt."
+
+doctest:
+ $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest
+ @echo "Testing of doctests in the sources finished, look at the " \
+ "results in $(BUILDDIR)/doctest/output.txt."
View
216 docs/source/conf.py
@@ -0,0 +1,216 @@
+# -*- coding: utf-8 -*-
+#
+# Zygote documentation build configuration file, created by
+# sphinx-quickstart on Fri Jun 24 18:18:33 2011.
+#
+# This file is execfile()d with the current directory set to its containing dir.
+#
+# Note that not all possible configuration values are present in this
+# autogenerated file.
+#
+# All configuration values have a default; values that are commented out
+# serve to show the default.
+
+import sys, os
+
+# If extensions (or modules to document with autodoc) are in another directory,
+# add these directories to sys.path here. If the directory is relative to the
+# documentation root, use os.path.abspath to make it absolute, like shown here.
+#sys.path.insert(0, os.path.abspath('.'))
+
+# -- General configuration -----------------------------------------------------
+
+# If your documentation needs a minimal Sphinx version, state it here.
+#needs_sphinx = '1.0'
+
+# Add any Sphinx extension module names here, as strings. They can be extensions
+# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
+extensions = []
+
+# Add any paths that contain templates here, relative to this directory.
+templates_path = ['_templates']
+
+# The suffix of source filenames.
+source_suffix = '.rst'
+
+# The encoding of source files.
+#source_encoding = 'utf-8-sig'
+
+# The master toctree document.
+master_doc = 'index'
+
+# General information about the project.
+project = u'Zygote'
+copyright = u'2011, Evan Klitzke'
+
+# The version info for the project you're documenting, acts as replacement for
+# |version| and |release|, also used in various other places throughout the
+# built documents.
+#
+# The short X.Y version.
+version = '0.1.0'
+# The full version, including alpha/beta/rc tags.
+release = '0.1.0'
+
+# The language for content autogenerated by Sphinx. Refer to documentation
+# for a list of supported languages.
+#language = None
+
+# There are two options for replacing |today|: either, you set today to some
+# non-false value, then it is used:
+#today = ''
+# Else, today_fmt is used as the format for a strftime call.
+#today_fmt = '%B %d, %Y'
+
+# List of patterns, relative to source directory, that match files and
+# directories to ignore when looking for source files.
+exclude_patterns = []
+
+# The reST default role (used for this markup: `text`) to use for all documents.
+#default_role = None
+
+# If true, '()' will be appended to :func: etc. cross-reference text.
+#add_function_parentheses = True
+
+# If true, the current module name will be prepended to all description
+# unit titles (such as .. function::).
+#add_module_names = True
+
+# If true, sectionauthor and moduleauthor directives will be shown in the
+# output. They are ignored by default.
+#show_authors = False
+
+# The name of the Pygments (syntax highlighting) style to use.
+pygments_style = 'sphinx'
+
+# A list of ignored prefixes for module index sorting.
+#modindex_common_prefix = []
+
+
+# -- Options for HTML output ---------------------------------------------------
+
+# The theme to use for HTML and HTML Help pages. See the documentation for
+# a list of builtin themes.
+html_theme = 'default'
+
+# Theme options are theme-specific and customize the look and feel of a theme
+# further. For a list of options available for each theme, see the
+# documentation.
+#html_theme_options = {}
+
+# Add any paths that contain custom themes here, relative to this directory.
+#html_theme_path = []
+
+# The name for this set of Sphinx documents. If None, it defaults to
+# "<project> v<release> documentation".
+#html_title = None
+
+# A shorter title for the navigation bar. Default is the same as html_title.
+#html_short_title = None
+
+# The name of an image file (relative to this directory) to place at the top
+# of the sidebar.
+#html_logo = None
+
+# The name of an image file (within the static path) to use as favicon of the
+# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
+# pixels large.
+#html_favicon = None
+
+# Add any paths that contain custom static files (such as style sheets) here,
+# relative to this directory. They are copied after the builtin static files,
+# so a file named "default.css" will overwrite the builtin "default.css".
+html_static_path = ['_static']
+
+# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
+# using the given strftime format.
+#html_last_updated_fmt = '%b %d, %Y'
+
+# If true, SmartyPants will be used to convert quotes and dashes to
+# typographically correct entities.
+#html_use_smartypants = True
+
+# Custom sidebar templates, maps document names to template names.
+#html_sidebars = {}
+
+# Additional templates that should be rendered to pages, maps page names to
+# template names.
+#html_additional_pages = {}
+
+# If false, no module index is generated.
+#html_domain_indices = True
+
+# If false, no index is generated.
+#html_use_index = True
+
+# If true, the index is split into individual pages for each letter.
+#html_split_index = False
+
+# If true, links to the reST sources are added to the pages.
+#html_show_sourcelink = True
+
+# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
+#html_show_sphinx = True
+
+# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
+#html_show_copyright = True
+
+# If true, an OpenSearch description file will be output, and all pages will
+# contain a <link> tag referring to it. The value of this option must be the
+# base URL from which the finished HTML is served.
+#html_use_opensearch = ''
+
+# This is the file name suffix for HTML files (e.g. ".xhtml").
+#html_file_suffix = None
+
+# Output file base name for HTML help builder.
+htmlhelp_basename = 'Zygotedoc'
+
+
+# -- Options for LaTeX output --------------------------------------------------
+
+# The paper size ('letter' or 'a4').
+#latex_paper_size = 'letter'
+
+# The font size ('10pt', '11pt' or '12pt').
+#latex_font_size = '10pt'
+
+# Grouping the document tree into LaTeX files. List of tuples
+# (source start file, target name, title, author, documentclass [howto/manual]).
+latex_documents = [
+ ('index', 'Zygote.tex', u'Zygote Documentation',
+ u'Evan Klitzke', 'manual'),
+]
+
+# The name of an image file (relative to this directory) to place at the top of
+# the title page.
+#latex_logo = None
+
+# For "manual" documents, if this is true, then toplevel headings are parts,
+# not chapters.
+#latex_use_parts = False
+
+# If true, show page references after internal links.
+#latex_show_pagerefs = False
+
+# If true, show URL addresses after external links.
+#latex_show_urls = False
+
+# Additional stuff for the LaTeX preamble.
+#latex_preamble = ''
+
+# Documents to append as an appendix to all manuals.
+#latex_appendices = []
+
+# If false, no module index is generated.
+#latex_domain_indices = True
+
+
+# -- Options for manual page output --------------------------------------------
+
+# One entry per manual page. List of tuples
+# (source start file, name, description, authors, manual section).
+man_pages = [
+ ('index', 'zygote', u'Zygote Documentation',
+ [u'Evan Klitzke'], 1)
+]
View
141 README.md → docs/source/index.rst
@@ -10,49 +10,49 @@ Let's say you're serving your application, and the currently deployed version is
called `A`. You're trying to deploy a new version of your web app, and that
version is called `B`. The way you want it to work is like this:
- * A new Python interpreter `P` starts up, imports code from `B` and does all of
- the static initialization and loads modules. This process should only happen
- once.
-
- * New HTTP workers are created by forking `P`. That way new workers don't need
- to reimport lots of code (so starting a worker is significantly cheaper in
- terms of disk I/O and CPU time), and workers can share static data structures
- (so starting a new worker consumes significantly less memory).
-
- * In progress requests that are being run from the `A` version of the code
- should be allowed to complete, and not be interrupted; deploying new code
- should not cause anyone to get an HTTP 500 response, or even be noticeable by
- users.
-
- * The deploy code needs to be cognizant of how many HTTP workers the system is
- capable of running (usually this means don't run more workers than you have
- RAM allocated for), so if a machine is capable of supporting 200 workers, and
- 100 of them are serving requests for `A` at the time of the deploy, at first
- the 100 idle `A` workers can be killed and 100 `B` workers can be spawned,
- and then `A` workers are killed and `B` workers are spawned as the `A`
- workers complete their requests.
+* A new Python interpreter `P` starts up, imports code from `B` and does all of
+ the static initialization and loads modules. This process should only happen
+ once.
+
+* New HTTP workers are created by forking `P`. That way new workers don't need
+ to reimport lots of code (so starting a worker is significantly cheaper in
+ terms of disk I/O and CPU time), and workers can share static data structures
+ (so starting a new worker consumes significantly less memory).
+
+* In progress requests that are being run from the `A` version of the code
+ should be allowed to complete, and not be interrupted; deploying new code
+ should not cause anyone to get an HTTP 500 response, or even be noticeable by
+ users.
+
+* The deploy code needs to be cognizant of how many HTTP workers the system is
+ capable of running (usually this means don't run more workers than you have
+ RAM allocated for), so if a machine is capable of supporting 200 workers, and
+ 100 of them are serving requests for `A` at the time of the deploy, at first
+ the 100 idle `A` workers can be killed and 100 `B` workers can be spawned,
+ and then `A` workers are killed and `B` workers are spawned as the `A`
+ workers complete their requests.
This is what Zygote does. Zygote has an embedded HTTP server based on the one
provided by Tornado, but this is complementary to a real, full-fledged HTTP
server like Apache or Nginx -- Zygote's expertise is just in managing Python web
processes. It's OK to run Apache or Nginx in front of Zygote.
Zygote is licensed under the
-[Apache Licence, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.html). You
+`Apache Licence, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0.html>`_. You
should find a copy of this license along with the Zygote source, in the
-`LICENSE` file.
+``LICENSE`` file.
How It Works
------------
The concept of "zygote" processes on Unix systems is not new; see Chromium's
-[LinuxZygote](http://code.google.com/p/chromium/wiki/LinuxZygote) wiki page for
+`LinuxZygote <http://code.google.com/p/chromium/wiki/LinuxZygote>`_ wiki page for
a description of how they're used in the Chromium browser. In the Zygote process
-model there is a process tree that looks something like this:
+model there is a process tree that looks something like this::
zygote-master
- \
- `--- zygote A
+ \
+ `--- zygote A
| `--- worker
| --- worker
|
@@ -79,7 +79,7 @@ fork.
Internally, communication between the different processes is done using abstract
unix domain sockets.
-If you use a command like `pstree` or `ps -eFH` you can verify that the process
+If you use a command like ``pstree`` or ``ps -eFH`` you can verify that the process
tree looks as expected. Additionally, if you have the `setproctitle` Python
module available, the processes will set their titles such that it's easy to see
what version of the code everything is running.
@@ -89,31 +89,32 @@ How to Use It
To use Zygote, you need to write a module that implements a `get_application`
method. That method takes no arguments, and returns an object that can be used
-by a [Tornado](http://www.tornadoweb.org/) HTTPServer object (typically this
+by a `Tornado <http://www.tornadoweb.org/>`_ HTTPServer object (typically this
would be an instance of `tornado.web.Application`).
-After that, an invocation of Zygote would be done like this:
+After that, an invocation of Zygote would be done like this::
python -m zygote.main -p 8000 -b ./example example
-Let's break that down. The `python -m zygote.main` part instructs Python to run
-Zygote's `main` module. The parts after that are options and arguments. The `-p
-8000` option instructs Zygote that your application will be served from port
-8000. The `-b ./example` option states that the symlink for your application
-exists at `./example`. This does not strictly need to be a symlink, but the code
-versioning will only work if it is a symlink. The final argument is just
-`example` and that states that the module name for the application is `example`.
+Let's break that down. The ``python -m zygote.main`` part instructs Python to
+run Zygote's `main` module. The parts after that are options and arguments. The
+``-p 8000`` option instructs Zygote that your application will be served from
+port 8000. The ``-b ./example`` option states that the symlink for your
+application exists at ``./example``. This does not strictly need to be a symlink,
+but the code versioning will only work if it is a symlink. The final argument is
+just ``example`` and that states that the module name for the application is
+``example``.
The example invocation given above will work if you run it from a clone of the
-Zygote source code. The `-b` option tells Zygote what to insert into `sys.path`
+Zygote source code. The ``-b`` option tells Zygote what to insert into `sys.path`
to make your code runnable, and in the Zygote source tree there's a file named
-`example/example.py`. In other words, `example` gets added to `sys.path` and
-that makes `example.py` importable by doing `import example`.
+``example/example.py``. In other words, `example` gets added to `sys.path` and
+that makes ``example.py`` importable by doing ``import example``.
Caveats
-------
-Currently Zygote only works with [Tornado](http://www.tornadoweb.org/)
+Currently Zygote only works with `Tornado <http://www.tornadoweb.org/>`_
applications. It should be fairly straightforward to get it working with other
WSGI webservers, however. It just requires someone whose willing to roll their
sleeves up and hack on the code a bit.
@@ -131,61 +132,65 @@ need.
Process Protocol
----------------
-The zygote master opens an abstract unix domain socket with a name like this:
+The zygote master opens an abstract unix domain socket with a name like this::
+
'\0' + "zygote_" + pid_of_master
-Messages to the master have the following format:
+
+Messages to the master have the following format::
+
str(pid_of_sender) + ' ' + msg_type + ' ' + msg_body
-The msg_type is a single byte, by convention it corresponds to an actual ASCII
-character. See `zygote/message.py` for the different message types.
-The master spawns zygotes. A zygote supports two signals. Sending it `SIGTERM`
-instructs it to exit. Sending the zygote `SIGUSR1` instructs the zygote to fork
-and start a worker process. The worker processes communicate to the zygote
+The ``msg_type`` is a single byte, by convention it corresponds to an actual
+ASCII character. See ``zygote/message.py`` for the different message types.
+
+The master spawns zygotes. A zygote supports two signals. Sending it ``SIGTERM``
+instructs it to exit. Sending the zygote ``SIGUSR1`` instructs the zygote to
+fork and start a worker process. The worker processes communicate to the zygote
master using the aforementioned abstract unix domain socket.
-Sending `SIGINT` or `SIGTERM` to a worker causes it to exit with status 0.
+Sending ``SIGINT`` or ``SIGTERM`` to a worker causes it to exit with status 0.
When a worker is spawned, it will send a "spawn" message to the master, signaled
-by `S`. The body of the "spawn" message is the PPID of the worker (i.e. the PID
-of the zygote that spawned the worker).
+by ``S``. The body of the "spawn" message is the PPID of the worker (i.e. the
+PID of the zygote that spawned the worker).
When a worker exits, its parent will send an "exit" message to the master,
-signaled by `X`. The body of the message will be of the format
-`str(pid_of_worker) + ' ' + str(exit_status)`. The master process will decide
-whether the zygote should respawn the worker or not (by sending `SIGUSR1` to the
-zygote if the worker should be respawned).
+signaled by ``X``. The body of the message will be of the format
+``str(pid_of_worker) + ' ' + str(exit_status)``. The master process will decide
+whether the zygote should respawn the worker or not (by sending ``SIGUSR1`` to
+the zygote if the worker should be respawned).
When a worker begins processing an HTTP request, it will send a "begin http"
-message, signaled by `B`. The body of the message will contain the request
-string sent by the client, so it will be something like `GET / HTTP/1.1`.
+message, signaled by ``B``. The body of the message will contain the request
+string sent by the client, so it will be something like ``GET / HTTP/1.1``.
When a worker finishes processing an HTTP request, it will send an "end http"
-message, signaled by `E`. There is no body.
+message, signaled by ``E``. There is no body.
While all of this is going on, the master processes operates a simple state
machine to keep track of the current status of all of the zygotes and worker
processes. It's up to the master process to know when it's safe to gracefully
kill a worker (which it can tell because the last message from the worker was an
-`S` or an `E`). It's up to the master process to keep track of how many requests
-a worker has processed, and whether that means the worker should be killed (and
-respawned). And so on. The implicit goal of this is that all complicated process
-management logic should exist in the zygote master; there should be very little
-logic in the zygotes, or in the worker children.
+``S`` or an ``E``). It's up to the master process to keep track of how many
+requests a worker has processed, and whether that means the worker should be
+killed (and respawned). And so on. The implicit goal of this is that all
+complicated process management logic should exist in the zygote master; there
+should be very little logic in the zygotes, or in the worker children.
Testing
-------
-There are unit tests, which exist in the `tests` directory. You should be able
-to run them by invoking `make test`, e.g.
+There are unit tests, which exist in the ``tests`` directory. You should be able
+to run them by invoking ``make test``, e.g.::
evan@zeno ~/code/zygote (master) $ make test
tests.test ZygoteTests.test_http_get ... ok in 2.53s
PASSED. 1 test / 1 case: 1 passed (0 unexpected), 0 failed (0 expected). (Total test time 2.53s)
-Some caveats. You need a very recent version of `tornado` to run the tests. This
+Some caveats. You need a very recent version of Tornado to run the tests. This
is to force Tornado to use the "simple" http client. Hopefully the API will be
-stable going forward from Tornado 0.2..
+stable going forward from Tornado 0.2.0.
-You will also need [testify](http://pypi.python.org/pypi/testify/) to run the
+You will also need `Testify <http://pypi.python.org/pypi/testify/>`_ to run the
tests. Any version of Testify should work.

0 comments on commit a9d8727

Please sign in to comment.