A Piece of CherryPy for CGI Programmers 193
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."
Umm.. (Score:3, Informative)
I have been doing that with python and perl for years. I can even run the CGI on multiple different hosts with one webserver.
Whoops - sorry, to head off any criticism: (Score:5, Informative)
What being in a single process really means (Score:5, Informative)
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.)
Re:What being in a single process really means (Score:3, Informative)
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.
Re:You mean... (Score:1, Informative)
Nevow (Score:5, Informative)
If you're doing any sort of network programming in Python, you need to look at Twisted.
Re:Seems only useful if you already do Python CGI. (Score:5, Informative)
Subway [python-hosting.com] 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".
Gee, it sounds just like... (Score:4, Informative)
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:
Re:Gee, it sounds just like... (Score:1, Informative)
Re:People are looking at this the wrong way (Score:3, Informative)
Actually, 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 [jython.org] for the latter, or The Tcl/Java Project [sourceforge.net], or The scheme package [koala.ilog.fr] for Java based scheme. lists some 200 languages that have been ported to the JVM.) [robert-tolksdorf.de]
Re:People are looking at this the wrong way (Score:3, Informative)
> that function is displayed in your web browser when you visit http://address/hello [address]
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.)
Re:People are looking at this the wrong way (Score:2, Informative)
Quixote does XMLRPC easier... (Score:1, Informative)
Re:People are looking at this the wrong way (Score:1, Informative)
Re:rm -Rf (Score:3, Informative)
Re:Bah, Scripting languages (Score:4, Informative)
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.
Re:" All it does is..." (Score:3, Informative)
Relatedly I just released Paste Deploy [pythonpaste.org], 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.
Re:You mean... (Score:3, Informative)
You may also want to check out lighttpd [lighttpd.org]. It's smaller and faster than apache on static content (dynamic content depends on cgi speed, I guess
Re:Bah, Scripting languages (Score:1, Informative)
Lisp, anyone?
Clue-stick (Score:1, Informative)
For instance, if your form has fields Name and Password with action="loginUser", you write this: And the arguments will be automatically passed. With CherryPy, your web app embeds its own multi-threaded web server; one practice is to put it behind Apache, so you can integrate it with already existing setups. It supports the Python Web Server Gateway Interface (WSGI) so you can use mod_python, IIS/ASP, FastCGI, SCGI , etc.
The ability to use Python makes web programming bearable again. Want something cool? SQLObject [sqlobject.org] turns your SQL (MySQL, PostGres, others) into Python objects. What's more, you don't have to define your class in code. You can read it straight from the DB schema: You can also combine CherryPy with your choice of templating system. And for version control, check out how Trac works with SVN at the CherryPy [cherrypy.org] site.
If you're sick of PHP, learn Python and enjoy programming again.
Quick fix for 2.1 (Score:3, Informative)
from cherrypy import cpg
to:
import cherrypy as cpg
More info here [cherrypy.org].
Re:Seems only useful if you already do Python CGI. (Score:3, Informative)
Re:Bah, Scripting languages (Score:1, Informative)
Nor would I be particularly surprised if Python were quite object oriented from the beginning,