捕获来自ipython的库f2py call stdout [英] Capturing library f2py call stdout from ipython

查看:174
本文介绍了捕获来自ipython的库f2py call stdout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有Python 3内核的Jupyter笔记本。

I'm using Jupyter notebook with a Python 3 kernel.

如果我运行:

import scipy.optimize
scipy.optimize.minimize(
    lambda _: 1,
    0,
    method='COBYLA',
    options={'iprint': 1, 'disp': True, 'maxiter': 2})



<我希望将诊断优化信息打印到ipython笔记本上。但是,这会打印到控制台。我怀疑是这种情况,因为优化例程在Fortran中实现,并通过f2py以scipy接口。 COBYLA Fortran文件执行实际打印。

I expect to get diagnostic optimization info printed to the ipython notebook. However, this prints to console. I suspect this is the case because the optimization routine is implemented in Fortran, and is interfaced in scipy through f2py. The COBYLA Fortran file does the actual printing.

如何将Fortran子例程输出传递给ipython笔记本?据我了解,它应该与调用编译的C函数相同 - 那么为什么不共享stdout?

How can I pipe the Fortran subroutine output to the ipython notebook? As I understand it, it should be the same as calling a compiled C function - so why isn't the stdout shared?

推荐答案

简短的回答是,你做不到。不容易。以下增强建议可涵盖的用例之一IPython / Jupyter协议。虽然它还没有被接受,也不会快速发生。

The short answer is that you can't. Not easily. That one of the use case that could be covered by the following enhancement proposal to the IPython/Jupyter protocol. Though it's not accepted yet, nor going to happen fast.

(手持式)原因是当使用Python时你可以monkeypatch sys.stdin / sys.stdout / sys.stderr并写入一个类似文件的界面重定向要做正确的事™,虽然当它是fortran / c / ...函数时,它们通常会直接打开与原始流相对应的文件句柄,但事后你不能改变它。

The (hand waved) reason is that when using Python you can monkeypatch sys.stdin/sys.stdout/sys.stderr and write to a file-like interface that redirect to do "The Right Thing"™, though when it's a fortran/c/... functions, they often do directly open the filehandles corresponding to the raw streams, and you can't change this after the fact.

唯一的解决方案是控制进程的启动方式,并提前更改文件描述符,因此建议使用内核nany。

The only solution is to control how the process get launched, and change the file descriptor ahead of time, hence the proposal for the "kernel nany".

让我们发展(在OP进一步提问之后)。

Let's develop (after OP further question).

Python print 是一个不直接打印到标准输出的函数,它确实写入 sys.stdout 除非另有说明。如果您签入普通的python shell:

Python print is a function which does not directly print to standard out, it does in fact write to sys.stdout unless told otherwise. If you check in a normal python shell:

>>> import sys
>>> sys.stdout
<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>

您可以看到它是文件句柄的直接包装。

You can see that it is a direct wrapper around a filehandle.

如果你在笔记本中做同样的事情(不是在IPython终端,这是另一个故事),你会看到< ipykernel.iostream.OutStream at 0x104602be0> 这是围绕ZMQ协议的代理对象。在IPython内核中,上一个流存储在 sys .__ stdout __ 中,因此您可以玩游戏并尝试

If you do the same in a notebook (not in an IPython terminal, that's another story), you will see <ipykernel.iostream.OutStream at 0x104602be0> which is a Proxy object around the ZMQ protocol. In an IPython kernel the previous stream is stored in sys.__stdout__ so you can play around and try a

sys.__stdout__.write('Hello StackOverflow\n')

哪个将在笔记本服务器的终端中打印Hello Stackoverflow。
不要忘记触发流刷新的 \ n

Which will print "Hello Stackoverflow" in your notebook server's terminal. Do not forget the \n which trigger the stream to be flushed.

不是这不是Jupyter行为,而是IPython行为。只要你通过ZMQ发送标准输出,Jupyter方面就不关心你是怎么做的。 Haskell内核可能通过提供它自己的 io 模块来做同样的事情。

Not that this is not a Jupyter behavior, it is an IPython behavior. The Jupyter side does not care how you do it, as long as you send the stdout over ZMQ. The Haskell kernel likely does the same by providing it's own io module.

捕获进程 stdout 是一个解决方案(内核保姆提案涵盖),但它有其自身的缺点。在Python级别重定向更简单,因为 sys.stdout 是为此做的。

Capturing the process stdout is one solution (that the kernel nanny proposal covers) but it has its own drawbacks. It is simpler to redirect at the Python level, as sys.stdout is made for that.

此行为是既不是bug也不是功能,可以说subprocess / f2py / pyc等......应该能够处理非标准的stdout / stderr作为参数,以及 nanny 是一种解决方法,可以帮助解决这些问题,这将是

This behavior is neither a bug nor a "Feature", one could argue that subprocess/f2py/pyc, etc... should be able to handle non-standard stdout/stderr, as argument, and the the nanny is a workaround to help with these case, which will be

这篇关于捕获来自ipython的库f2py call stdout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆