Why avoid CGI for Python with LAMP hosting?

前端 未结 6 524
春和景丽
春和景丽 2021-01-31 04:52

I have been using PHP for years. Lately I\'ve come across numerous forum posts stating that PHP is outdated, that modern programming languages are easier, more

相关标签:
6条回答
  • 2021-01-31 04:59

    Really it's just an efficiency thing - CGI spawns an entire new process for every request, which is quite heavyweight for what it does.

    PHP can be run through CGI as well, but mod_php embeds the interpreter within apache. There's a mod_python which does the same job, and mod_wsgi as Yuval says.

    0 讨论(0)
  • 2021-01-31 05:00

    mod_wsgi is the proper alternative. It is preferable over CGI in almost all aspects.

    0 讨论(0)
  • 2021-01-31 05:05

    Classic CGI isn't the best way to use anything at all. With classic CGI server has to spawn a new process for every request.

    As for Python, you have few alternatives:

    • mod_wsgi
    • mod_python
    • fastcgi
    • standalone Python web server (built-in, CherryPy, Tracd )
    • standalone Python web server on non-standard port and mod_proxy in Apache
    0 讨论(0)
  • 2021-01-31 05:07

    There is a page in the Python documentation that describes the advantages and disadvantages of the various possibilities.

    mod_python

    (…) These are the reasons why mod_python should be avoided when writing new programs.

    WSGI

    The Web Server Gateway Interface or WSGI for short is currently the best possible way to Python web programming. While it is great for programmers writing frameworks, the normal person does not need to get in direct contact with it.

    FastCGI and stuff

    These days, FastCGI is never used directly.

    0 讨论(0)
  • 2021-01-31 05:13

    Aside from the suggestions others make, you should really consider using a framework of some kind. You can and should be using FastCGI, mod_python, or mod_wsgi, but they weren't really intended for you to write code directly against. Might I suggest one of the following?

    • django (my favorite for practical applications)
    • pylons
    • cherrypy (my favorite for not-so-practical applications)
    • web.py
    0 讨论(0)
  • 2021-01-31 05:19

    Why is it that using CGI is not the best way to use Python?

    I will stick up for CGI a little. It's good for development environments.

    It's simple to wire up and you don't have to worry about module reloading problems. Naturally performance is terrible, but for dev you don't care.

    Of course you should really be writing to the WSGI interface rather than CGI directly. You can then deploy through CGI using:

    wsgiref.handlers.CGIHandler().run(application)
    

    and use the same application object to deploy through mod_wsgi other whatever other WSGI server you prefer in the production environment where speed matters (and the testing environment where you want it to be as close to production as possible).

    0 讨论(0)
提交回复
热议问题