的
BadFileDescriptor误差和因此存储器访问冲突由以下事实引起
stdout的在窗口模式下的应用程序是一个固定大小的缓冲区。因此,如果您正在
stdout使用
sys.stdout直接写入,一段时间后您会看到这些错误。
您可以通过以下方法解决此问题:
- 删除/注释上的著作
stdout
- 使用
logging
而不是打印到标准输出 stdout
在应用程序执行开始时进行重定向。尽管我认为将调试语句移至logging
将是更好的选择,但这是一种解决方案,只需更改较少的代码。
要重定向,
stdout您可以使用以下代码:
import sysimport tempfilesys.stdout = tempfile.TemporaryFile()sys.stderr = tempfile.TemporaryFile()
在执行程序之前。您还可以使用一些自定义对象将输出放入“ log”文件或其他文件中,重要的是输出不应填充固定大小的缓冲区。
例如,您可以执行以下 *** 作以在
logging不更改太多代码的情况下利用模块:
import sysimport loggingdebug_logger = logging.getLogger('debug')debug_logger.write = debug_logger.debug #consider all prints as debug informationdebug_logger.flush = lambda: None # this may be called when printing#debug_logger.setLevel(logging.DEBUG) #activate debug logger outputsys.stdout = debug_logger
这种方法的缺点是对每一行
stdout.write:
>>> print 'test'DEBUG:debug:testDEBUG:debug:
如果愿意,可以避免编写纯
write函数
the_logger.debug仅用“全行”调用的实际行为。
无论如何,我认为这类解决方案应该只是临时的,并且只能在将
logging.debug。
(显然,记录器应写入文件,而不是
stdout避免错误。)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)