`
flysnowxf
  • 浏览: 572241 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

shell后台执行serve_forever()报出Input/output error

阅读更多
http_server.py为一个启动http服务器的脚本,大概代码:
server_address = ("", 7000)
server = BaseHTTPServer.HTTPServer(server_address, ServerHandler)
server.serve_forever()


现象:
1)在linux shell后台执行python脚本
python http_server.py &

2)退出当前shell
exit
3)这时请求一下http服务
http://localhost:7000
4)报错
Traceback (most recent call last):
  File "http_server.py", line 57, in <module>
    server.serve_forever()
  File "/usr/local/lib/python2.6/SocketServer.py", line 226, in serve_forever
    self._handle_request_noblock()
IOError: [Errno 5] Input/output error

原因:
server.serve_forever()启动后,任何一个访问,都会输出访问历史记录:
localhost - - [07/Sep/2011 15:21:24] "GET / HTTP/1.1" 200 -
这个log的输出,需要输出的载体,这里是依赖shell。如果此时退出了shell,log输出找不到地方,就会报出以上错误。
而且很诡异的是,这个log我怀疑是以error为输出管道,而不是标准的input输出管道,因为我尝试过python http_server.py > /dev/null &一样会报错。

解决:
既然缺少了log输出载体,我们就指定一个,这里使用/dev/null,传说中的垃圾回收站。
执行脚本使用:
python http_server.py > /dev/null 2>&1 &

标准和error输出都扔到/dev/null

针对此现象的一个朋友的描述:
http://hi.baidu.com/dalier/blog/item/bb9429301b60d290a9018e9f.html
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics