Thoughts on search engine,cloud,big data,linux,java,lucene,hadoop

Pyramid FAQ: from the beginning to production deployment

Filed under: python,troubleshooting — Tags: — euler @ 10:07 pm February 12, 2012

I started to write pyramid applications 3 weeks ago, as a python and pyramid novice.  Now the application goes to production.  In the development process, many errors and problems happened to the application, although the pyramid documentation is very good, some latest changes are not included in the reference document.

I wrote down the common problems I came across ,  and the solution I chose against the problem here.

1. How to install pyramid?

For help getting Pyramid set up, try the official install guide.

Also you may be interested at my pyramid installation guide.

2. Is there a quick start?

Yes, there is. To get the feel of how a Pyramid web application is created, go to the single file tasks tutorial page on the official site.

But be careful, the single file application is just for learning. It is single threaded which is not suitable for most production deployment. And it is not good for maintenance when all content in a single file.

3. How could I create a project for production deployment?

A scaffold project could be created for production deployment. A scaffold project means a template project. You can create project from scaffold.  See the outdated official wiki tutorial for a general guide, the tutorial is for pyramid 1.2 which is different from 1.3. For current pyramid 1.3, you should read this document: Pyramid documentation 1.3 . I’ve written  A quick start for python pyramid for quick reference of pyramid project creation.

4.When I create project by the wiki tutorial of Pyramid and run paster create xxx, it shows command not found?

Paster may not be installed. And if you are using pyraid 1.3 paster is not needed any more, see official Pyramid documentation 1.3 for detail.

5. The wiki tutorial said run “paster serve development.ini” to start pyramid. But paster does not exist.

Paster serve is replaced by pserve in pyramid 1.3.

$ ../bin/pserve development.ini

6. When I run “pserve development.ini” to start pyramid by Pyramid documentation 1.3. It shows error: “pkg_resources.DistributionNotFound: waitress”.

In pyramid 1.3a7, a new dependency waitress is introduced. You can install the packed by easy_install or pip: pip install waitress

7.When I start pyramid by pserve, it allows visiting of only 1 concurrent user.

pserve is single threaded, and is for development purpose. For multithreaded production deployment, you should take one of these wsgi server.

8. Is there a simple way to deploy pyramid as multithreaded application?

Yes, you can Gunicorn to deploy pyramid as multithreaded application.

Firstly install gunicorn and eventlet:

$easy_install gunicorn eventlet

Then create file runapp.py as the following lines:

from paste.deploy import loadapp 

application = loadapp('config:production.ini', relative_to='.')

Then run the following command to start pyramid as multithreaded application:

$gunicorn --workers=2 --worker-class=eventlet --bind 127.0.0.1:80 runapp

the worker-class means to start gunicorn in async mode(multi threaded).

 

And for single threaded deployment, you can easily run Gunicorn with `pserve production.ini` if you’ve been using with Waitress. Just replace the [server:main] section within production.ini as shown in the document: http://gunicorn.org/run.html#paster-serve

http://gunicorn.org/design.html#contents

http://gunicorn.org/configure.html#worker-class

 

Digg This
Reddit This
Stumble Now!
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

4 Comments

  • There are a number of problems with your introduction, I’d suggest hopping into #pyramid on freenode to chat about them. In particular, your suggested gunicorn setup can fail in strange and mysterious ways due to the interaction between eventlets and commonly used C modules such as psycopg2.

    Comment by Richard Clark — February 13, 2012 @ 5:19 am

  • “pserve” is not a WSGI server; it’s just a script that starts a WSGI server. That means the sentence where you say “pserve is single threaded” doesn’t make much sense. If you mean that Waitress is single-threaded, it’s not either. wsgiref is indeed single threaded, but Pyramid never renders any scaffolding that uses it.

    If someone creates scaffolding from scratch using Pyramid 1.3, they won’t need to install Waitress by hand. You had to because you had scaffolding generated under 1.2 and you were trying to use it under 1.3, I suppose.

    THe way you’re suggesting to start gunicorn is not a multithreaded mode. gunicorn actually does not have a multithreaded mode, AFAIK. Instead it has 2 other ways to do concurrency: single-threaded async using gevent and multiprocess (sync workers config). You’re suggesting to use the single-threaded async variant. This will work most of the time but does require special support from database adapters and other things.

    Comment by Chris McDonough — February 13, 2012 @ 6:24 am

  • Thanks, Richard, good suggest.

    Comment by euler [weibo] — February 13, 2012 @ 2:49 pm

  • Chris, Thank you very much for the detail interpretation. I’ll learn more about Pyramid and WSGI. And then revise the tutorial.

    Comment by euler [weibo] — February 13, 2012 @ 2:55 pm

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.