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."
This is nothing special. Just another framework that doesn't really do anything unique at all...
units.smile.face.width = "Miles"; smile.face.width = "10";
You are not the customer.
OK, enough with the silly Py names. PyCrust, PyShell, and now CherryPy, sheesh.
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?
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.
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.
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.
CGI's my cherry pie.. A cool drink of water...
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.
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.
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.
"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 [address] "
And the security issue?
CherryPy implements some useful ideas, but no more so than various other Python frameworks available out there. There are after all only so many ways of mapping URLs into a Python based framework, and there are currently a lot of Python based web frameworks, thus a lot of duplication.
:-) :-) :-)
It is sad to say, but at the moment the war as to which is perceived as being the better Python framework for web applications is being won more on the marketing side than on the particular technical merits and quality of implementation.
There are some people out there who are developing some really quite interesting stuff based on inovative ideas and who are producing good quality code as well, but in many cases, although they may result in a better experience as far as developing a web application, because they don't have the associated marketing that some projects have, they get passed over and the projects never get developed that extra step to turn them into something awesome.
The ultimate in hype for Python web frameworks of late has been Django. It has been getting a huge amount of mind share by comparing itself to Ruby on Rails. The last time I looked they still hadn't actually made an official release.
In summary, if you want to build a successful Open Source project, don't start by writing code, do what any commercial business does and ratchet up the hype through marketing and promise the world. Do that and you will be almost certain to get lots of people to help you with the code once you do start.
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.
So, in other words, it performs poorly on multi-processor machines? Or, is it threaded? (I don't know if threaded would count as single-process. Sure it counts in terms of terminology, but in the eyes of the kernel threads are just another form of process.)
And it shares resources with other instances of itself and other webapps. For something that gets used a lot (say, Slashdot) I'd imagine it'd run out of file descriptors and/or memory pretty quickly.
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.
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?
Irritable, left-wing and possibly humorous bumper stickers and t-shirts
What is the best or most stable method then?
I see this program is unstable and only supports ssh1 sort of. What is the most stable method of using a web interface to connect to a shell.
Lets say like using www to ssh or even some thing like cgi.irc wich uses a web interface to a shell then irc's.
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.
Interactive Visual Medical Dictionary
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:
- "History shows again and again how nature points out the folly of men" -- Blue Oyster Cult, 'Godzilla'
Easy as pie?
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)
...
-Brendan
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.
Why not just embed the python interpreter in an apache module ....
Only 'flamers' flame!
Does slashdot hate my posts?
I can see it now.
http://www.site.com/rmdir
http://www.site.com/mke2fs?/dev/sda
http://www.site.com/kill `pidof httpd`
when you see the word 'Linux', drink!
There are too many scripting languages...actually there are too many interpreters for scripting languages.
.NET is really great in that way.
Except for the syntax differences, what is the difference between ruby,perl,php and python?
Seeing as they are so similar why should I have to install four different interpreters and why should applications have to be ported between the languages?
Seems like a large waste of time.
I'm all for lots of versions of the same sort of tool as long as they are compatible.
This is some thing microsoft got right.
Many languages, one interpreter.
- Jessta
...and that is all I have to say about that.
http://jessta.id.au
...how about a piece of ApplePy?
As an alternative, Quixote http://www.mems-exchange.org/software/ has a slightly different flavor to exposing Python objects via the url, probably a little more secure out of the box (including cross site scripting protection) and does XMLRPC in a straightforward manner.
Hoo-ray for options!
heh. no, i think you missed the /. hype train. Posted Aug 02
sounds familiar...
Do you even lift?
These aren't the 'roids you're looking for.
Welcome to 1998.
Hey, I'm just your average shit and piss factory.
Great, so it's vulnerable to mass injection attacks. You COULD do this in Java Reflection. But you wouldn't. Ever.
Hey, I'm just your average shit and piss factory.
it's barely still virgin
"Our interests are to see if we can't scale it up to something more exciting," he said.
"Please step out of the booth, sir. We have a warrant for your dessert."
It's:
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!
Man this is bad but when I read the header to this article, the first thing that came to mind was Warrant ...
I run a project that aims to be a java port of CherryPy. Supports all free vms and is very lightweight.
OOWeb
You realise this is hardly anything new as well, right?
Of course. I never said it was new idea :-). CherryPy itself is not a very new framework either. It's been around for a while. It's just one of the clean frameworks out there in an over engineering world.
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 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 site.
If you're sick of PHP, learn Python and enjoy programming again.
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?
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
I used CherryPy to turn the public domain US Department of Agriculture's nutrient database in to a profitable website, Food File Online. The single script used to power it represents just under one weeks work!
One of the major things that makes Python acceptable is that it's ability to use C or C++ code as modules.
This is basicly how it goes..
You write the program to be 'correct' in python. Since python is very quick programming language to program with this initial stage can be done much quicker then with traditional languages such as C++ or Java. Generally python programmer's generate programs faster and with less bugs then other programmers.
Then when you get the program finished and it's tested it may be to slow for the workload. Python is a fast language to program, but it produces slower programs sometimes. (although memory use is much much less then thru normal vitual machine languages like Java)
There are some simple 'magic' you can sprinkle around like Psyco to generate faster code, but often that's not quite enough.
What you do then is you do profiling on the actual program. That way you find out what _actual_ parts of the program take up the most resources.
Often on programming your forced to guess what parts of the code will be slow and spend more time on those portions. With python and the ability for rapid profiling you can find out on a living breathing program what parts are slow.
Then you take those parts and optimize them by cleaner/better code or thru rewriting those portions in a C or C++ and incorporating that functionality back into the program as modules.
That way you end up with a Python program that is just as fast as anything else because you've been able to more accurately determine what parts of the program needs the most work and time spent on it.
The drawback is that there is a significant amount of "glue" that is needed to build python modules so it's not as straightforward as it sounds at first... but there are some very nice tools to help with that.
For instance you have Boost.Python, and of course SWIG.
Then there is more cool stuff, like Psyco, which can increase math operations substantially... But one interesting one is Pyrex...
Pyrex allows you to write 'C' modules by using a special language that is a combination of most legal python with most legal C. Work is ongoing for C++-like support. It takes what you program, turns it to C then it is compiled using the normal GCC compiler. Very interesting for quickly getting solid modules built.
That's the advantage of Python. It allows you to use those CPU cycles to your advantage, but doesn't abuse them. If your a experianced C programmer then python is a complete breeze and you don't have to abandon your skills to learn the 'corporate'-hyped languages.
It's very cool language for newbies and gurus alike and not only ties very well into the Unix enviroment, but is very Windows-freindly too.
For isntance you can take Python code with numerious third-party add-ons and dependancies and such and turn it all into a single *.exe standalone executable for windows enviroment.
The link to the CherryPy website is http://www.cherrypy.org/
Maybe the post should be updated to include this.
This is what the Zope server, at its very guts in the ZPublisher module, does.
Of course, CherryPy is a more recent construction, but the basic concept isn't all that surprising.
Lacks threading though. Try this:
from cherrypy import cpg
class App:
@cpg.expose
def index( self ):
from time import sleep
sleep( 15 )
yield "done"
@cpg.expose
def test( self ):
yield "test"
cpg.root = App()
cpg.server.start()
Point your browser at http://localhost:8080/
and while index() is sleep()'ing, try to open
http://localhost:8080/test/ in another window.
It won't respond until previous request answers.
Rails and Struts already have this feature.
Don't forget Catalyst!
Of course, being later to the game allowed Catalyst to one-up the competition with the utmost in flexibility: full regex-based URL handlers!
bp
If I had moderator points right now, I would mod you funny, because you can't seriously be saying that you dislike having a hard time seeing your url space with CherryPy, and in the same breath recommend that mangled vomit of regex instead. :)
Mahnamahna!
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.
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.
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).
You don't have to learn every framework to keep your resume up to date. If you are a competant programmer, you can pick up a new framework in a few days. If you know python already, learning cherrypy is trivial, and cherrypy isn't resume worthy anyways. That's like those idiots who put "word" as a skill on their resume. If you can't figure out how to work a different word processor then you shouldn't be looking for a job in IT.
And if a webhost is any good, they don't have to keep everything up to date for you, you can install stuff in your home dir and use it there. That's what you have to do when you want to use a beta version of something anyhow.
Python is faster than perl. And its an order of magnitude faster than ruby, and rails seems to be giving everyone a permament boner lately.
Frankly I don't think python is a contender for CGI programming. Nor do I think perl or any other interpreted language is. Stop using CGI, its 2005 for fuck's sake.
"ASP.NET is lightyears ahead of anything python simply because it gets compiled into machine code"
.NET's VM runtime ). Am i wrong or isn't it just when that bytecode gets loaded and handled to a JIT ( just-in-time ) compiler that it gets actually compiled to native machine code?
/. runs Perl, don't you?
Machine code, yes. Only it is machine code ( bytecode ) for the CLR (
BTW, i worked about a year on a system upgraded from ASP ( interpreted VBScript ) to ASP.NET ( bytecode compiled C# ). We wrote it from scratch, and only used the same DB stored procedures the original system also used. Well, guess what, smarty?! Performance sucked big time! The simple, interpreted ASP application was faster and less bloated.
BTW, you know such a high-traffic as
"before it's executed and runs "closer to the iron" (and therefore blows the doors off anything interpreted)"
yea, my ass it is.
I don't feel like it...
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
but it improves performance and gains persistence across requests by handling all its requests within a single process
Persistance between browswer invocations can only be done via a cookie, hidden form variable or ip (which is not reliable). So saying that the single process gives you magical persistance is misleading - no? This same can be done with cgi simple and via apache modules.
On thing a persistent single process can do for you is bring down the house with a memory leak.
Mozilla users get Hot Sauce at a discount.
---------------
The difference between MSIL and Java bytecode is that MSIL was created to be quickly JITted before execution. Read up on this technology it's quite fascinating. Methods get compiled as son as they're called, on the fly and remain compiled into machine code for the life of the process. So you're running ONLY machine code when executing a page within ASP.NET. Heck, you can even pre-JIT your DLLs if you feel adventurous.
Java bytecode, on the other hand was created to be run inside a VM, which means interpreted. Compiling Java into machine code is a lot more difficult and processor intensive a process as compiling MSIL. This is why only parts of Java bytecode get JITted by even the most recent VMs.
Did you reply to the wrong post by accident or something? What does anything you are saying have to do with my post?