views:

1094

answers:

4

Hello all,

I'm having a very peculiar problem in my Python FastCGI code - sys.stdout has a file descriptor of '-1', so I can't write to it. I'm checking this at the first line of my program, so I know it's not any of my code changing it.

I've tried sys.stdout = os.fdopen(1, 'w'), but anything written there won't get to my browser.

The same application works without difficulty under Apache.

I'm using the Microsoft-provided FastCGI extension for IIS documented here: http://learn.iis.net/page.aspx/248/configuring-fastcgi-extension-for-iis60/

I am using these settings in fcgiext.ini:

    ExePath=C:\Python23\python.exe
    Arguments=-u C:\app\app_wsgi.py
    FlushNamedPipe=1
    RequestTimeout=45
    IdleTimeout=120
    ActivityTimeout=30

Can anyone tell what's wrong or tell me where I should look to find out?

All suggestions greatly appreciated...

+1  A: 

Forgive me if this is a dumb question, but I notice this line in your config file:

Arguments=-u C:\app\app_wsgi.py

Are you running a WSGI application or a FastCGI app? There is a difference. In WSGI, writing to stdout isn't a good idea. Your program should have an application object that can be called with an environment dict and a start_response function (for more info, see PEP 333). At any rate, your application's method of returning will be to return an iterable object that contains the response body, not writing to stdout.

Either way, you should also consider using isapi-wsgi. I've never used it myself, but I hear good things about it.

Jason Baker
Not a dumb question at all - I should have mentioned this in my question...`app_wsgi.py` invokes a FastCGI library which then calls my WSGI app. It's the FastCGI library that is having problems writing to stdout.
Ronan Klyne
A: 

On windows, it's possible to launch a proces without a valid stdin and stdout. For example, if you execute a python script with pythonw.exe, the stdout is invdalid and if you insist on writing to it, it will block after 140 characters or something.

Writing to another destination than stdout looks like the safest solution.

Bluebird75
I'm already using python.exe to try to ensure that I'm getting a valid stdout. I've got a valid stdin that can read data, but not a valid stdout...Unfortunately, the FastCGI spec says the server is listening for me to write FastCGI data on stdout, so I can't just change the way I do it...
Ronan Klyne
A: 

Following the PEP 333 you can try to log to environ['wsgi.errors'] which is usualy the logger of the web server itself when you use fastcgi. Of course this is only available when a request is called but not during application startup.

You can get an example in the pylons code: http://pylonshq.com/docs/en/0.9.7/logging/#logging-to-wsgi-errors

gawel
+1  A: 

Do you have to use FastCGI? If not, you may want to try a ISAPI WSGI method. I have had success using:

http://code.google.com/p/isapi-wsgi/

and have also used PyISAPIe in the past:

http://sourceforge.net/apps/trac/pyisapie

Randy Syring
Is the can't most more that 1 URL a self-imposed or SO imposed constraint ? Anyway ... http://sourceforge.net/apps/trac/pyisapie
whatnick
SO imposed since I was a new user, but I fixed it now
Randy Syring