IIS下Python FastCGI - stdout写入问题
我的 Python FastCGI 代码中遇到一个非常特殊的问题 - sys.stdout 的文件描述符为“-1”,所以我无法写入它。 我在程序的第一行检查这一点,所以我知道我的代码没有改变它。
我尝试过 sys.stdout = os.fdopen(1, 'w'),但其中写入的任何内容都不会到达我的浏览器。
相同的应用程序在 Apache 下运行毫无困难。
我正在使用此处记录的 Microsoft 提供的 IIS FastCGI 扩展: http://learn.iis.net/page.aspx/248/configuring-fastcgi-extension-for-iis60/
我在 fcgiext.ini 中使用这些设置:
ExePath=C:\Python23\python.exe Arguments=-u C:\app\app_wsgi.py FlushNamedPipe=1 RequestTimeout=45 IdleTimeout=120 ActivityTimeout=30
任何人都可以告诉我出了什么问题吗?告诉我应该去哪里寻找答案?
非常感谢所有建议...
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果这是一个愚蠢的问题,请原谅我,但我注意到您的配置文件中的这一行:
您运行的是 WSGI 应用程序还是 FastCGI 应用程序? 存在差异。。 在 WSGI 中,写入标准输出并不是一个好主意。 您的程序应该有一个可以使用环境字典和 start_response 函数调用的应用程序对象(有关详细信息,请参阅 PEP 333)。 无论如何,应用程序的返回方法将返回一个包含响应正文的可迭代对象,而不是写入标准输出。
无论哪种方式,您还应该考虑使用 isapi-wsgi。 我自己从未使用过它,但我听说它很好。
Forgive me if this is a dumb question, but I notice this line in your config file:
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.
必须使用FastCGI吗? 如果没有,您可能想尝试 ISAPI WSGI 方法。 我已经成功使用:
http://code.google.com/p/isapi-wsgi /
并且过去也使用过 PyISAPIe:
http://sourceforge.net/apps/ trac/pyisapie
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
我相信标准输出关闭/无效符合 FastCGI 规范:
I believe having stdout closed/invalid is in accordance to the FastCGI spec:
在 Windows 上,可以在没有有效标准输入和标准输出的情况下启动进程。 例如,如果您使用 pythonw.exe 执行 python 脚本,则 stdout 为 invdalid,如果您坚持写入它,它将在 140 个字符或其他字符后阻塞。
写入标准输出之外的另一个目的地看起来是最安全的解决方案。
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.
遵循 PEP 333,您可以尝试登录到 environ['wsgi.errors'],当您使用 fastcgi 时,它通常是 Web 服务器本身的记录器。 当然,这仅在调用请求时可用,但在应用程序启动期间不可用。
您可以在 pylons 代码中获取示例: http ://pylonshq.com/docs/en/0.9.7/logging/#logging-to-wsgi-errors
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