Slashdot Mirror


A Piece of CherryPy for CGI Programmers

An anonymous reader writes "IBM developerWorks is running an article outlining the strengths and offering some helpful advice on the Python framework 'CherryPy'. CherryPy uses the same concepts as CGI to bind a web server to a web application, but it improves performance and gains persistence across requests by handling all its requests within a single process."

50 of 193 comments (clear)

  1. You mean... by nxtw · · Score: 5, Insightful
    it runs a web application in a signle process, like ASP, ASP.NET, and most likely other technologies have been doing for some time?

    This is nothing special. Just another framework that doesn't really do anything unique at all...

    1. Re:You mean... by Jerf · · Score: 2, Insightful

      With the staggering number of frameworks available, "unique" is an awfully high bar. I'm not sure a standard that eliminates all frameworks from consideration is a valuable one.

    2. Re:You mean... by spectral · · Score: 2, Insightful

      You mean like.. fastcgi? and then you aren't tied to your language?

    3. Re:You mean... by damiangerous · · Score: 2, Insightful

      At no point in the article does this claim to be "unique" in any way. Quite the opposite, in fact. It acknowledges that there has been no shortage of these frameworks. The only way in which it's "unique" is that its Python. Basically, it exists simply so that people who know and enjoy Python can stick with it.

    4. Re:You mean... by jma05 · · Score: 5, Interesting

      CherryPy is nothing like ASP, ASP.NET. The script you write is EVERYTHING. You don't need a web server like you do in ASP/ASP.NET. It is the web server with your code in it. I have been using CherryPy for a while. What is nice about it is that for simple things, it justs steps out of the way. There is very little framework code in my apps and they just feel like console programs.

    5. Re:You mean... by marcello_dl · · Score: 2, Informative

      >I use fastcgi with perl all the time. I love it, makes my apps very fast. Apache 2.0's "fcgi" module (fastcgi server component, but better maintained) works nicely almost out of the box.

      You may also want to check out lighttpd. It's smaller and faster than apache on static content (dynamic content depends on cgi speed, I guess ;) )

      --
      ---- MISSING MISCELLANEOUS DATA SEGMENT --- [sigdash] trolololol
  2. Obligatory by Limburgher · · Score: 4, Funny

    units.smile.face.width = "Miles"; smile.face.width = "10";

    --

    You are not the customer.

    1. Re:Obligatory by Anonymous Coward · · Score: 2, Insightful

      I hate to be anal about a joke, even if it doesn't really qualify as one, but shouldn't that be more like this?

      face.smile.width.units = "Miles"
      face.smile.width = 10

    2. Re:Obligatory by Roberto · · Score: 3, Insightful

      How hard is it to try it? Not hard.

      [GCC 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)] on linux2
      Type "help", "copyright", "credits" or "license" for more information.
      >>> a=1;b=2
      >>> a
      1
      >>> b
      2
      >>>

  3. PyDie by Anonymous Coward · · Score: 2, Funny

    OK, enough with the silly Py names. PyCrust, PyShell, and now CherryPy, sheesh.

  4. So, we now have by TDScott · · Score: 3, Insightful

    Regular CGI, mod_perl, mod_python, the newcomer Ruby on Rails, and now CherryPy. Granted, some webhosts handle the first four (even Rails) without any problems, but how many do we really need?

    I suppose the answer is "as many as it takes" — whatever's easiest for some users will be utterly impenetrable to others, and it's good to have choice. But at what point does it start to become a burden to keep up with all these — either for programmers looking to keep their CVs up to date, or hosts wanting to stay current?

  5. Umm.. by Anonymous Coward · · Score: 3, Informative

    FastCGI anyone?

    I have been doing that with python and perl for years. I can even run the CGI on multiple different hosts with one webserver.

    1. Re:Umm.. by dotgain · · Score: 2, Interesting
      Sure, FastCGI is great, I agree. But, even after all these years "Fast" and "Python" still aren't allowed to be used in the same sentence, hence CherryPy. Mark me Troll, that's fine, but please tell me: Am I the only one that thinks CGI in python is insane because it's the all round slowest-to-run programming language about? Sure it's nice to write and easier to read than most, but for any program you end up running 100 times more often than you write it, choose somthing else.

      Can anybody show me that Python is a realistic contender for CGI - or for that matter - any programming? I used to subject myself to a Linux distribution called Gentoo whose package mismanagement system, Portage, is mostly if not entirely done in Python. I found that for compiles of many small packages, we spent about as many CPU seconds on 'emerge' itself than we did building. Running 'emerge' on something old and spluttery like a sparc32 is just not the done thing. You've got probably 30 seconds before you've got any indication that the program has validated your input and started processing at which point you find something else to do.

  6. Whoops - sorry, to head off any criticism: by TDScott · · Score: 4, Informative

    that's a referral link in the parent post. To be honest, I'd recommend them anyway, but it's probably best to disclose it.

  7. Give me a reason to use this by wilsoniya · · Score: 2, Interesting

    I'm not a py programmer at all, but seeing as numerous single-process dynamic web platforms exist (PHP, JSP/Servlets), whats w/ all the hype? Maybe ppl are just happy to be able to use python for web apps?

    -m

    --
    I can't remember the last time I forgot anything.
    1. Re:Give me a reason to use this by Poromenos1 · · Score: 3, Interesting

      Probably... Personally I love Python, and one of the things I love is that it's still being developed. You can actually suggest something and see it implemented in the language. You can probably do that in many more, but all the others I know have been standardised. Plus it has all the other great stuff which makes it my first choice for anything.

      --
      Send email from the afterlife! Write your e-will at Dead Man's Switch.
    2. Re:Give me a reason to use this by Nasarius · · Score: 4, Interesting

      There are already efficient ways of using Python for web apps. FastCGI and mod_python, for example. I'd like to see benchmarks for this new one.

      --
      LOAD "SIG",8,1
  8. CGI's my cherry pie by computerjunkie · · Score: 3, Funny

    CGI's my cherry pie.. A cool drink of water...

  9. What being in a single process really means by MostlyHarmless · · Score: 5, Informative

    In the first few posts, I've seen a lot of relatively lacking-in-clue replies asking how CherryPy is different from ASP.NET, mod_python, FastCGI, etc. With most Apache-based web platforms, one process will handle many requests, but you cannot guarantee that every request will be handled by the same process: by default, apache starts multiple (possibly multi-threaded) servers, and creates and destroys them as necessary.

    CherryPy, on the other hand, runs every request from the same process by using a thread pool instead of a process pool. This means that any global variables you change will be visible to any request. In many cases (keeping in mind memory restraints), you can share items in memory that would otherwise have to go through the database, which can help performance and make keeping track of state easier. Of course, multithreaded data sharing places its own demand on the programmer: the Python core is inherently thread-safe, but no programming language can protect you from race conditions and the like.

    I've played around a little bit with CherryPy, and writing in it definitely feels Pythonic. It may still need some more development before it is fully mature, but it's something to at least keep an eye on.

    (On a side note: I don't know how the IIS/ASP.NET process model works. It does let you store data across an application, but you are limited to a single Application hashtable, probably to be orthogonal to the Session and Viewstate objects and to reduce the likelihood that a programmer not experienced with concurrency would shoot him/herself in the foot.)

    --
    Friends don't let friends misuse the subjunctive.
    1. Re:What being in a single process really means by nxtw · · Score: 2, Informative
      In the first few posts, I've seen a lot of relatively lacking-in-clue replies asking how CherryPy is different from ASP.NET, mod_python, FastCGI, etc. With most Apache-based web platforms, one process will handle many requests, but you cannot guarantee that every request will be handled by the same process: by default, apache starts multiple (possibly multi-threaded) servers, and creates and destroys them as necessary.

      ASP.NET uses a single process for each application. Every request is processed by the same process, both in Mono's implementation with mod_mono and on the IIS implementation. This is, as I said, nothing special, uncommon, or unique. My post was definitely not lacking in clue.

      CherryPy, on the other hand, runs every request from the same process by using a thread pool instead of a process pool. This means that any global variables you change will be visible to any request. In many cases (keeping in mind memory restraints), you can share items in memory that would otherwise have to go through the database, which can help performance and make keeping track of state easier. Of course, multithreaded data sharing places its own demand on the programmer: the Python core is inherently thread-safe, but no programming language can protect you from race conditions and the like.

      ASP.NET runs every request for an application within the same worker process. Static classes can be accessed from all threads throughout the application's lifetime (until it is stopped).

      (On a side note: I don't know how the IIS/ASP.NET process model works.

      Proving that you are indeed "lacking-in-clue"...

      It does let you store data across an application, but you are limited to a single Application hashtable

      This is not the only way to store data across an application in ASP.NET. Static classes, variables, etc. stick around for the entire time the application is running. ASP.NET also has its Cache object, which works like Application except Cached objects will expire at a programmer-defined time or after a programmer-defined timespan. The Cache object allows for callbacks for when an object is removed from the Cache, and cache dependencies.

      I believe that ASP.NET's page and partial page caching uses this.

  10. People are looking at this the wrong way by sean23007 · · Score: 5, Interesting

    I think people are looking at this the wrong way. I see a lot of posts saying "who cares? ASP is already like that!" or "You're supposed to have it in a single process anyway!"

    What makes this cooler is that Python functions are exposed in the URL. Read through that IBM tutorial. It's fairly interesting. Put a function called hello() in your CherryPy application, and the return value of that function is displayed in your web browser when you visit http://address/hello

    I don't know about you, but I think that's pretty cool. You could definitely do some interesting stuff with this, and I can see it saving a lot of time in the code-writing phase. And once you get your head wrapped around that concept pretty well, the design phase would probably get a lot shorter too. (Not to mention how much easier it would then become to add new features to the application.)

    This is interesting for two reasons: Python frameworks are now catching up to things like ASP and PHP, but are doing some crucial things differently that might make it much easier/more powerful. I might start using this instead of PHP for small web apps that just need to talk to a database, and see how it goes from there.

    --

    Lack of eloquence does not denote lack of intelligence, though they often coincide.
    1. Re:People are looking at this the wrong way by civilizedINTENSITY · · Score: 2, Informative
      "is lightyears ahead of anything python simply because it gets compiled into machine code"

      Actually,
      .NET introduced a new programming language environment that compiles all source code into an intermediate language. .NET languages are compiled into the Microsoft Intermediate Language (MSIL), which is executed by the Common Language Runtime (CLR) software in the Windows computer. The MSIL is similar to Java's bytecode, except that whereas Java is one language, .NET supports multiple programming languages...
      Now that is also not quite true, because people have written various JVM bytecode compilers for their languages, or implemented their languages in Java. (See Jython for the latter, or The Tcl/Java Project, or The scheme package for Java based scheme. lists some 200 languages that have been ported to the JVM.)
    2. Re:People are looking at this the wrong way by Beek · · Score: 3, Informative

      > Put a function called hello() in your CherryPy application, and the return value of
      > that function is displayed in your web browser when you visit http://address/hello

      Rails and Struts already have this feature.

      (Of course, for Struts you need a some XML for every class you want to act this way.)

    3. Re:People are looking at this the wrong way by ziplux · · Score: 2, Informative

      Someone is, and it's called Subway: http://subway.python-hosting.com/

    4. Re:People are looking at this the wrong way by jma05 · · Score: 4, Insightful

      For crying out loud, please read the article/manual. Zope and CherryPy have NOTHING in common with Rails other than they are web app frameworks using dynamic languages. They are all different frameworks addressing different needs. Zope is very complex because it addresses a very complex problem and provides a comprehensive framework. CherryPy is similar to Rails in that functions become URLs seemlessly (no messy xml, special output calls etc) but other than that it is much simpler than Rails. It is an web application server and nothing more. It does not have a templating system, no ORM, no database abstraction -- NOTHING. All it does is manage your code for the web. You choose whatever templating system you want (I chose Cheetah), whatever ORM you want (I chose SQLObject), and whatever DB access module (I chose MySQLdb). It is a simple tool that imposes no restrictions and plays well with other tools. Development in CherryPy is similar to development with mod_python although the infrastructure is completly different. Sometimes CherryPy is easier to develop with than mod_python. STOP COMPARING EVERYTHING WITH RAILS BECAUSE THAT IS ALL YOU HEAR IN THE NEWS.

    5. Re:People are looking at this the wrong way by Ian+Bicking · · Score: 2, Insightful
      Python frameworks are now catching up to things like ASP and PHP
      While Python web programming has some issues, "catching up" isn't really how I'd describe it. Even in the most basic of Python frameworks you get more expressive power than in ASP and PHP. You also get equivalent performance to PHP (I can't say much about ASP -- but Python is fast enough which is all that matters). In terms of expressiveness Python beats the pants off PHP, Visual Basic, C#, and Java. There's no "catching up" there at all, they were long since passed.

      But I'd certainly agree the single process thing is a red herring; it's not what makes CherryPy interesting.

    6. Re:People are looking at this the wrong way by stuntpope · · Score: 2, Interesting

      I love all this "x has that already" talk when CherryPy has been around for years before some of the x's.

  11. Cool! by smoondog · · Score: 4, Interesting

    Python is a great language, but my worry is about security. I would think that given the previously mentioned cool features, this app would have more security worries than your average all-in-the-same-process cgi extenders.

    1. Re:Cool! by hoka · · Score: 3, Interesting

      What I've encounted with Python is the overwhelming lack of crypto, not necessarily a lack of "security" (I use that term loosely here). What stems from the lack of good default crypto is a sense of a lack of security. For example, the Python SSL implementation has no certificate verification, which is a huge problem for anybody who wants to do any client work. There is also a complete lack of support in most libs for servers (https server? secure xmlrpc server?). This means that you can only half-ass the client side, and the server side is non-existant. While I havn't looked at the framework, I'm hoping that they implemented extra features into the system that makes it a bit better of a choice for security reasons. Right now I'm having a hell of a time trying to get some basic solid crypto security going over xmlrpc.

  12. Nevow by kevin_conaway · · Score: 4, Informative

    I'll got ahead and put in a plug for Nevow here, another web framework that is based on the EXCELLENT Twisted framework.

    If you're doing any sort of network programming in Python, you need to look at Twisted.

  13. " All it does is..." by LionKimbro · · Score: 5, Interesting

    It says "All it does is connect the Web server to your Python code with as little fuss as possible. It doesn't make decisions about what other tools to use, ..."

    And then in the very next paragraph, it says: "Instead of relying on Apache or another Web server, CherryPy runs its own small Python-based Web server."

    No, no, no!

    I love CherryPy as a way of routing requests to Python objects and functions. Rock on!

    But look, I'm running like 20 wiki and 5 custom web apps and a few WordPress installations on my server.

    And they are all plugged into Apache.

    So, actually, in fact, CherryPy has now made some decisions about what tools I'm supposed to use.

    Sure, I can forward requests from Apache to the CherryPy server, but that is yet another hassle, it is yet another thing to support and maintain and think about.

    I wish instead that the CherryPy dev's had made it so there were multiple adapters to the CherryPy system.

    All that said:

    CherryPy is my favorite system for doing web apps in Python. I've used it, I've loved it, it's great. It does make programming WebApps "fun," which is perverse. So, it's succeeded.

    But I strongly dislike how I have to do this funny Apache business to get it to run on port 80, or I have to give people weird 8080 addresses, like you saw in the article.

    Another thing I dislike, is that it's kind of tricky to get it to do XML-RPC, in my experience. (Then again, that was 3 months ago. Perhaps things have changed now.)

    (I just use AutoXmlRpcServer or AutoXmlRpcCgi for when it's XML-RPC alone, without a web side along with it.)

    But again: CherryPy is my favorite, when there is no XML-RPC aspect, and when I don't mind the weird config stuff I have to do to get it to cooperate with Apache.

    1. Re:" All it does is..." by Anonymous Coward · · Score: 2, Funny

      Perhaps.

      You could put more than one thought.

      On each line.

    2. Re:" All it does is..." by Ian+Bicking · · Score: 2, Informative
      The Python WSGI spec (PEP 333) is trying to address exactly this issue. And I agree it's an issue -- I maintain quite a lot of Python applications (not in CherryPy, but same basic setup) and it can be really annoying. CherryPy supports WSGI, though its WSGI support isn't perfect (but they're aware and working on it).

      Relatedly I just released Paste Deploy, which is also intended to address these issues. It's just a small piece that will only be useful given more infrastructure support, but I'm optimistic. In the end I think it will mean some specific setup to support Python applications on a server, but once the basic support is configured adding an application should be a one-line affair.

  14. Seems only useful if you already do Python CGI. by mcc · · Score: 2, Insightful

    All it does is connect the Web server to your Python code with as little fuss as possible. It doesn't make decisions about what other tools to use, so you're free to pick a templating system, database mapper, or other tool on its own terms.

    This is kind of a problem though because I actually need a templating system, database mapper, and some other tools. I have some such tools in Perl, but I obviously can't take these with me into Python.

    So I am wondering. Were one to use CherryPy, what would be logical tools to build on top of it with? If I need to be able to take objects and convert them into lines in a database or HTML for display or HTML forms for editing or whatnot, what would be the logical things to plug in on top of CherryPy to provide this?

    1. Re:Seems only useful if you already do Python CGI. by justrob · · Score: 5, Informative

      Using SQLObject is very popular with CherryPy users. CherryPy works with just about any templating system out there. This also makes it very easy to port from other Python web frameworks because you can use your existing templates.
        Subway was created to use CherryPy, SQLObject and Cheetah templates in a very Ruby on Rails-like way, so you don't have to go through the 10 zillion decisions of what to use with CherryPy and tells you "what to do and where to put it".

    2. Re:Seems only useful if you already do Python CGI. by Ian+Bicking · · Score: 2, Informative

      Leonard Richardson, who wrote the article in question, also recently wrote articles on SQLObject and Cheetah.

  15. People are looking at this the wrong way by darekana · · Score: 3, Interesting

    Actually I think CherryPy and Zope have been around a while before Ruby on Rails. And speed wise Python kicks Ruby's ass for now.

    And actually I don't know of any java frameworks that don't require tons o' xml flinging to get up and running. Please feel free to enlighten me.

  16. Gee, it sounds just like... by mengel · · Score: 3, Informative
    ... FastCGI which has had a several python modules for about 10 years...

    But of course, if IBM says it's new, well it must be ;-)

    Okay, I checked, and I exagerrated a little bit, the earliest CVS version on mod_fastcgi.c is:

    Revision 1.1 / (download) - annotate - [select for diffs] , Tue Sep 16 15:38:22 1997 UTC (7 years, 11 months ago) by stanleyg
    --
    - "History shows again and again how nature points out the folly of men" -- Blue Oyster Cult, 'Godzilla'
  17. django! (/. missed the hype train) by brendano · · Score: 5, Interesting
    The big thing in python web programming right now is the introduction of Django, a mature RAD framework that shares lots of features with Rails. It's got a lot going for it; it'll be interesting to see how things turn out.

    I find CherryPy's URL traversal scheme a bit clunky -- since you connect up objects to each other via attributes, you can't see the hierarchy of your site. At least with PHP you can use "ls" to discover what your URL space looks like. Django uses a really neat scheme that binds a table of named regular expressions to callable handlers, e.g.

    (r'^polls/(?P<poll_id>\d+)/$', 'myproject.apps.polls.views.polls.detail')

    and the handler is declared as

    def detail(request, poll_id)
    ...

    ...so that requesting /polls/13/ maps to calling detail(request, 13). Here's more about it...

    --
    -Brendan
  18. Wow, dude. Chill. by jbellis · · Score: 3, Insightful

    If you're "running like 20 wiki and 5 custom web apps and a few WordPress installations" on your server then you shouldn't be intimidated by the 2 or 3 lines it takes to forward requests to the CherryPy server.

    Get a grip.

  19. For windows users... by Jeffus · · Score: 2, Funny

    ...how about a piece of ApplePy?

  20. Re:rm -Rf by sean23007 · · Score: 2, Informative

    I'm assuming you didn't read the article, because it explains that CherryPy doesn't just call whatever function is passed in the URL. The programmer has to explicitly expose his functions to the CherryPy server instance. Otherwise, they won't be called. So unless for some reason you @cpg.expose rmdir or kill or any other system function (which you would obviously never do) this isn't an issue.

    --

    Lack of eloquence does not denote lack of intelligence, though they often coincide.
  21. Re:Wow, dude. Chill. by LionKimbro · · Score: 2, Interesting
    It's not just the 2 or 3 lines in the Apache server.

    It's:

    • The 2 or 3 lines in the Apache server.
    • Learning where in the Apache documentation those 2 or 3 lines are, and familiarizing myself with them.
    • Debugging those 2 or 3 lines, fiddling with Apache a bunch.
    • Figuring out the details of where the CherryPy server is going to run, with what priviledges, with what permissions on what directories.
    • Figuring out how to configure the CherryPy server.
    • Figuring out how to make the CherryPy server automatically start when the computer boots up.
    • Making the changes so that it autostarts when the computer boots.
    • And then testing that it autostarts when the computer reboots, by actually rebooting it.


    I just plain don't like any of that stuff. None of it.

    I'm not a sysadmin; I'm lucky to have cobbled together my 20 wiki, 5 custom web apps, and a few WordPress installs. I dread upgrading my Wordpress blogs, one of which isn't even working right now, and has been custom hacked. Something about not being able to connect with the MySQL db for some reason, I don't know. I don't even care at this point. I dislike diddling with stuff.

    Gimme as few pieces as possible. Don't make me think about security, don't make me make things automatically start at boot time, plug into my existing framework, yadda yadda yadda.

    Gimme gimme gimmy!
  22. 80's flashback by QuantumMajo · · Score: 2, Funny

    Man this is bad but when I read the header to this article, the first thing that came to mind was Warrant ...

  23. Re:Bah, Scripting languages by masklinn · · Score: 3, Informative
    Python is an Object Oriented language experiment. An interpreted language that can be compiled. Based on the Java ideal without the "Everything is an Object", clearer syntax than Perl, and more consistency within the syntax (than Java).

    Beeep, wrong

    Python is not based on any "java ideal", and everything in python is indeed an object, a module is an object, a function is an object, and "1" is an object by itself.

    And it can't be compiled, it's a purely interpreted language, it's merely loosely syntax-checked and translated to bytecode (not compiled mind you, that's several steps under java). Some utils write machine code at "compile time" (Psycho), but it's not built in the base language in any way.

    --
    "The way we can tell it's C# instead of Haskell is because it's nine lines instead of two." -- wadler
  24. Plodding Python? by crucini · · Score: 2, Interesting

    I don't know much about Python, but I doubt that emerge's slowness comes from Python. It's probably doing a linear scan of something that should be indexed more intelligently. Or maybe it's opening a huge number of files.

    A similar thing applies to web apps - a slow web app is usually due to high-level design errors, not a slow scripting language. A lot of web apps spend most of their time in database calls. When an app is blocked on I/O, it's just as fast in Python as in C, much like a Porsche stuck next to a Yugo in traffic.

    Perl is not exactly a speed champ compared to C, but many snappy web pages run on Perl because the amount of computation involved in sending the web page is relatively small. Is Python substantially slower than other scripting languages?

  25. Re:Wow, dude. Chill. by Anonymous Coward · · Score: 2, Insightful

    What your post boils down to is you complaining that in order to use something new, you have to learn something new.

    Well, duh.

    Anyway it's all here: CherryPy behind Apache

  26. Quick fix for 2.1 by mav[LAG] · · Score: 2, Informative

    If you're like me and downloaded the latest cherrypy to follow along with the article, there's a quick fix that will make version 2.1 work with it. Just change any lines that say:

    from cherrypy import cpg

    to:

    import cherrypy as cpg

    More info here.

    --
    --- Hot Shot City is particularly good.
  27. Re:django! (/. missed the hype train) by Ian+Bicking · · Score: 2, Interesting
    If we're talking about URL management, an interesting Python project is a port of Rails' Routes. This is notable in particular because it is reversable -- you can both unpack a URL to a controller and set of arguments, and pack a controller and set of arguments back into a URL.

    It's a very new project, so I don't think it's been used in any Python framework yet. But it would probably be applicable to quite a few of them (perhaps including CherryPy).

  28. she's my..... by tq_at_sju · · Score: 2, Funny

    she's my cherryPy looks just like a stand alone app but its a cccccccggggggiiiiiii...sweet cherryPy

    --
    http://www.vanillaafro.com - take me seriously and I will shoot you