views:

386

answers:

4

Hello,

This question is running a danger of being marked as already asked. I've went through similar posts on stackoverflow and other sites and was not able to find an exact answer.

I am planning to write a simple web service which will be an interface to a cloud storage such as S3. In addition it will perform various simple validations on every access using a database. The service will use lighttpd with FastCGI. Although the service itself will not be complicated the load on it could be very high.

I am currently trying to decide whether to use PHP or Python to write this web service. I've read a number posts comparing the languages on ease of use and speed, but so far nothing jumps out as a clear indication of which one should be better suited for the problem.

From your experience which language would you select assuming that the application will be written from scratch, should be easily maintainble and extendable, have good performance, preferably some unit tests, etc.

I understand this is a bit vague, please let me know how I can clarify it to make the question clearer.

Thank you!

+6  A: 

Update: the issue of Python performance is irrational, even religious. Take Performance Comparison – C++ / Java / Python / Ruby/ Jython / JRuby / Groovy as an example.

Such comparisons typically make several mistakes.

  1. More often than not PHP is compared without nn opcode cache like APC (the fix is in);
  2. Comparisons are made on things like (as in this example) pure language syntax, like this example is all non-API code (creating a ton of temporary objects in this case). That again gives you a warped view because in the case of PHP, the API functions are really fast so at best such tests are incomplete, at worst it's deliberately biased;
  3. Such tests typically bear no resemblance to real-world usage; and
  4. Such performance figures are basically irrelevant because they're completely dwarfed by network latency for real code.

Also, if you were going to go on such a benchmark, you'd use Java. Somehow Java is faster than C++ in this test? There's another reason it's suspect.

Google searches for holy grail of Python performance should be another indicator here. Would Google be looking for performance improvements if there wasn't significant room for improvement?

Now I have nothing against Python. It's a fine language and an entirely reasonable choice but this zealotry of Python fanboys and the particular axe some have to grind with PHP in particular is nothing short of ridiculous and nauseating.

So pick whichever one you or your team is most familiar with. If you're not familiar with any then there is no wrong choice.

I'm a big fan of PHP for several reasons:

  • Maturity: it's been around for 15+ years;
  • Scalability: PHP runs 4 of the 20 largest sites on the Internet (Facebook, Wikipedia, Flickr and Yahoo);
  • Low Barrier to Entry: it's easy to get into PHP;
  • Suitability: PHP is Web-focused and that's what it's used for 99% of the time;
  • Fast and Broad API: there is a huge set of API functions that do pretty much everything you want and those calls are fast as they're a compiled module;
  • Cheap to Host: PHP costs almost nothing. This is more of an issue when you compare it to, say, Java hosting.
  • Opcode caching: this eliminates most of any performance criticisms of non-API PHP code.

Basically PHP is proven (like it or not).

Note: these aren't advantages of PHP over Python (as one of the comments questioned). These are simply positives for PHP.

PHP of course has negatives too (inconsistent parameter syntax, inconsistent use of underscores in API functions, etc) but these don't really matter other than possibly offending one's delicate sensibilities. Some might point to PHP being not really object-oriented as a negative. While the observation is true (see PHP is not object oriented!) I dispute that this is a negative. It's just a different way of doing things. There is no inherent or absolute superiority in OO techniques.

cletus
Thank you for your answer.At this moment I am equally not familiar with either language. I am both trying to pick the best langugage for the problem which would be the language I will then learn.
Alex
The "core API" (the runtime engine and built-ins) are typically all C code in both PHP and CPython (and Java in JVM implementations of either, etc), so the second paragraph is really peculiar.
Alex Martelli
btw, regarding the PHP core faster part, you can use psyco and Python has the advantage. Or, you can have a look at unladen swallow, which is going to rock the Python world soon: http://code.google.com/p/unladen-swallow/.
Prody
I'm not sure all of your reasons make sense. The maturity one is a bit suspect, since Python has been around for longer. Scalability is misleading because all of those sites are all built using a shared-nothing, horizontally scaled approach. The language doesn't matter. Low barrier to entry is subjective. It's just as easy to get into python. And the point about Google and Unladen Swallow definitely begs the question. Surely Google chose Python initially for reasons beyond performance? Why didn't they choose PHP?
RibaldEddie
@RibaldEddie: you're misperceiving my list of PHP positives as a list of PHP advantages over Python, which it is not. They're simply PHP positives.
cletus
Just a remark: I assume Facebook to be a well visited site. As far as I know (although I don't know much about the internals) most of it is written in PHP. So PHP seems to be least fast enough to handle such a site ;)
Felix Kling
Id say PHP is easier to get stared wih, but maybe harder to master.
OIS
Performance Comparison – C++ / Java / Python / Ruby/ Jython / JRuby / Groovy: is closed for replies but: 1. The object Person is flawed because it should not know about previous/next Person in a chain and 2. It should use the PHP array not some Chain class.
OIS
@cletus, fair enough regarding PHP's positives, but the original question was about comparing the two. Your answer doesn't compare the two, it just states some things about PHP.
RibaldEddie
@RibaldEddie: my position is such PHP vs Python comparisons are an irrelevant distraction.
cletus
+8  A: 

Objectively speaking, I'm quite confident in saying that Python is superior in every conceivable way, but I'm sure you will find people who will disagree. If you are starting from scratch, I really don't know why a person would choose PHP.

Most of the benchmarks I've seen show Python to be significantly faster than PHP.

Okay people seem to be clamoring for how I came to my objective soapbox, so I will repeat what I said in the comments below:

Python provides more language features for making a developer's life easier. For example, it allows for easy functional programming constructs such as lambda functions. It also provides for multiple inheritance. As well you can change the inheritance hierarchy of an object after the object has been instantiated. Further, you can give function parameters names and use those names in the callers to make your code more readable and avoid the ugly PHP requirement of passing in a bunch of default variables to a method just to set the nth parameter to some non-default value.

Python, in many benchmarks is faster. It has a more robust memory management system and garbage collector. PHP has no garbage collector, it uses reference counting to determine which memory to free. This can result in memory leaks in long running programs with significant class and object dependencies/hierarchies.

Arguably, Python has a larger and more sophisticated base of applications, frameworks, and plugins. For example, SQLAlchemy is an extremely powerful ORM and Database Abstraction Layer. There really isn't anything equivalent in PHP.

Python is an older language. One could argue that its age means that it's had more time to mature and get better.

I'm sure there are other reasons as well, and I'm also sure that you can write pretty much whatever you need in PHP as well as in Python. But looking at what each language provides, IMHO, shows Python to be better.

And one more edit before I head out: Late static binding was added to PHP in 5.3 but it's still sort of half-assed (http://www.digitalsandwich.com/archives/65-Late-static-binding....sorta.html). That's another feature that exists in other languages like Python but just seems to be lacking something in PHP.

Okay, I just have to see this through because I am amazed at the response that has come up from my claim that python is better.

There seems to be a recurring theme amongst some software developers that every language, practice, and method is always covered by a YMMV-type belief. But it seems to me that this can sometimes masquerade an unwillingness to take a stand at best and naive relativism at worst.

The question asks which is better for web services development, python or PHP. I made the claim that it is objectively true that python is better than PHP for this task and I stand by that claim. I even go so far as to claim that python is general better than PHP.

Some people believe that it's an unfair statement, that somehow it's not possible for one to be objectively better than the other. I strongly disagree and I definitely believe that one language can be shown to be better than another both for a specific use and in general terms. Indeed, this must be so otherwise the phrase "use the right tool for the job" would be meaningless; i.e., if there is a right tool then there must by definition be a wrong tool.

Python is, in general terms, more "right" as a software development language, including and especially for a FastCGI-based web service, for several reasons which I have tried to make clear. These include, but are not limited to:

  1. More robust memory management and far better garbage collection, especially for long running processes, which if FastCGI is used, the process will indeed be long running. PHP is notorious for leaking memory. Obviously it has gotten better in this regard lately but it is still a significant risk for someone running an FCGI PHP process.
  2. Better and more seamless Unicode and multibyte character support. PHP is notoriously horrible in this regard. None of the built-in PHP string functions are multibyte safe and even a simple str_replace() can mangle your multibyte strings. Yes, I know that you can use mb_string_* replacement functions but it is still a weakness and not every built-in function has an mb_string replacement (str_replace is a case in point).
  3. Python has far more support for both Object Oriented Programming and Functional programming. Python's OOP features far surpass PHP's, including as mentioned dynamic and flexible inheritance hierarchies and late static binding. While not every developer will use these features, the fact that they are in the language and robust gives you more options and more flexibility. Python's functional programming constructs are also missing from PHP.
  4. Python has several third-party libraries that are second-to-none. Django, SQLAlchemy, and Albatross are just a few libraries that are highly regarded in the development community. SQLAlchemy especially provides features that don't exist elsewhere, or are more cumbersome, even in the big corporate languages like C# and Java. The OP wants to build a web service layer-- I'm not sure there's anything in the PHP world that would compare with the elegance and flexibility of Albatross + SQLAlchemy. That is a significantly powerful combination that I would wholeheartedly recommend to anyone wanting to build any significant web services infrastructure.
    1. Python has other syntactic sugar that gives it more flexibility and power than PHP. I mentioned at least one of these features earlier, named parameters, that I miss often in PHP.

The base criteria that I have suggested include language features, runtime robustness, and available third-party libraries. Note that in order for python to be better than PHP, it does not require PHP to be considered completely awful that no person in their right mind would ever use (though I do personally wonder why someone would build a green-field project in it), but when you are asked to make a comparison by the question and you have to determine criteria by which to make that comparison I don't see how you can argue with the claim that language features, runtime robustness, and third-party libraries are a bad criteria.

Objectively, Python has more language features than PHP for both OOP and functional programming. That should not be contentious--- you can make a list and count the bullet points.

Objectively, the python runtime is more robust. It has more sophisticated memory management, and PHP is known to still to this day have memory leaks that can make long-running processes exit with an out of memory error.

I suppose you could take me up on the third-party library claim, but if you do know of anything in PHP that compares to either Albatross or SQLAlchemy for the poster's desires, PLEASE for the love of god let me know what it is because I need to start using it right away in my own PHP projects.

RibaldEddie
http://arstechnica.com/open-source/news/2009/03/google-launches-project-to-boost-python-performance-by-5x.ars
cletus
*Objectively speaking*? Your comment is completely devoid of any objectivity. It's pretty close to flamebait actually. You might want to provide said benchmarks so you don't get downvoted into oblivion.
zombat
http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/ A short benchmark. I've come across others, I will try to find them.
RibaldEddie
Okay, objectively, Python support more language features all around, including various syntactic sugars. Named function parameters, multiple inheritance, ability to change inheritance tree at runtime, anonymous functions that aren't a pain to create. Ever try running a PHP script as a daemon? Can you say memory leaks?
RibaldEddie
Unicode support! Is it really that hard to believe that Python is better than PHP? Really that hard? Come on Blake!
RibaldEddie
RibaldEddie
I agree with zombat: there is nothing objective here and it is rampant (and naive) fanboyism at its best. I've got nothing against Python but the upvotes for this answer are a classic case of voting up a poor answer just because you happen to agree with it.
cletus
Programming languages have features, all other things being equal. no particular feature is necessary to get the job done, but if a language provides those features, should you need them, it strikes me as being a bit naive to say that the one with more features is "as good" as the one with fewer. For example, you may not NEED named parameters to get your job done but it helps make your code more readable and more maintainable. That to me is worth something objectively valuable.
RibaldEddie
So I'm not sure it's fanboyism as much as looking at what the language provides and being able to say: "hey that's a neat thing to have, maybe it can make my software better and my job more fun." I don't think that's a huge stretch and I don't think you can argue that Python doesn't have more of those sorts of features than PHP.
RibaldEddie
Your 'objective' answer has zero facts nor citations. Well done.
Mike B
lol @“objectively speaking”! I'm tempted to +1 for sheer chutzpah :-)
bobince
I am not convinced by your statement (at all), please show us some statistical benchmark you ran/saw to support yourself.
Jay Zeng
downvoted for heavy bias
OIS
How can I get downvoted for bias when the question was "From your experience which language would you select"? The question is ASKING us for our biases for god sakes!
RibaldEddie
"From your experience" isn't the same as "be completely biased".
cletus
Excellent point you have made here......thanks
+7  A: 

You can accomplish all the specific goals you have mentioned in either language. There have been an uncountable number of threads about which language is better than the other, but there is nothing about either one that says "you should always use this one because of X". If that was the case, no one would use the worse language.

Use whichever one will allow you to complete the project fastest and with the highest quality code, based on your skillset and knowledge.

zombat
Agreed, though there is one practical reason you *might* prefer Python, for services that are going to be handling a bunch of non-ASCII text. PHP's Unicode support is generally poor.
bobince
This doesn't answer the QUESTION: "From your experience which language would you select"!
RibaldEddie
+3  A: 

I know someone who works at a major social networking company. That company built their business on PHP; they are now desperately trying to cope with the horrible problems that PHP has when you try to scale it.

According to the guy I know, PHP is very poorly designed. It's easy to get started with PHP, and it works great for small sites with low traffic, but if you succeed and get lots of traffic (which should be your goal) then PHP becomes a problem.

Since you have no investment in either PHP or Python, I firmly believe Python is the better choice. Also, Python gets you Django, and Django is an excellent system for web service development. Django is one of the ways you can put your application on Google App Engine, although I haven't used that and I have read (here on Stack Overflow) that the GAE version of Django is missing some features compared to the basic Django.

Python already outperforms PHP, and Google is investing some serious resources into a much-faster version of Python (Google search "unladen swallow" to read more about it).

With Python, you are using the same language used for physics modeling, AI and robotics research, serious math, etc. It's an outstanding language, very polished. Once you learn Python, you can use it for other things; whereas PHP is only used for web services.

I recommend Python.

EDIT: By the way, The Django Book has a chapter on how to set up a Django web service for handling heavy load. It talks about how to use memcached and how to scale up to multiple servers. I wrote a web service once in Django, I followed their advice, and it all worked out very well.

steveha
which big sites use phyton?
OIS
Reddit and Youtube use Python: http://en.wikipedia.org/wiki/List_of_Python_software#Commercial_usesWashingtonPost.com uses Django and Python: http://opensource.washingtontimes.com/blog/post/coordt/2009/02/washington-times-releases-open-source-projects/Don't know how many are big, but here are over 2800 sites using Django: http://www.djangosites.org/
steveha
@OIS I'm pretty sure that Blogger is a python site. It certainly was some time ago. YouTube as well. In fact many Google owned sites use Python or did at one point in time.
RibaldEddie