我使用R的pipe()函数来捕获shell命令的输出,但我也希望从命令中获取退出代码.
我知道我可以system2在这里使用,但我需要管道的优势,即能够以流方式处理输出.
我正在考虑编写自己的库来包装popen()和pclose()C函数,以利用pclose()返回退出状态的事实,但也许这可以避免.
有什么建议?谢谢!
注意
有一些方法可以使用临时文件,命名管道等来实现这一点,但我最好还是避免这些解决方法.我愿意在其中编译一个带有R-> C函数的共享库(我甚至愿意复制粘贴部分R源代码),但我不愿意重建R.
更新
我开始阅读R源代码,发现未经检查的pclose调用:
在src/main/connections.c:
static void pipe_close(Rconnection con)
{
pclose(((Rfileconn)(con->private))->fp);
con->isopen = FALSE;
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用实现R_pclose复制R代码的C函数的方法,close()但保存此返回值.我很遗憾地遇到了这个静态变量src/main/connections.c
static Rconnection Connections[NCONNECTIONS];
Run Code Online (Sandbox Code Playgroud)
因为我必须运行objcopy --globalize-symbol=Connections libR.so /path/to/my/libR.so才能访问变量,看起来我最好的解决方案是使用我自己的补丁重建R来捕获pclose返回值.
我有来自第三方的一组绝对庞大的.a文件.我有自己的库,只从这组库中调用5或6个函数.我想生成一个小得多的.a文件,其中包含我的代码及其(少量)依赖项在外部库中.
具体来说:
external.h
int foo();
int bar();
Run Code Online (Sandbox Code Playgroud)libexternal.a
0000000000000000 T foo()
0000000000000010 T bar()
Run Code Online (Sandbox Code Playgroud)mylibrary.c
#include "external.h"
int foo_wrapper() { return foo(); }
Run Code Online (Sandbox Code Playgroud)mylibrary.h
int foo_wrapper();
Run Code Online (Sandbox Code Playgroud)我想创建一个文件libmylibrary.a,其中包含符号foo_wrapper,依赖关系foo以及foo内部调用的任何内容,但不是bar(实际上代表数千个函数).通过这种方式,人们可以在不必包含external.h或链接的情况下包含我的库libexternal.a.我不愿意手动列出,foo因为实际上有很多功能.
可以吗?
我愿意列出我需要的符号.所以例如,我可以foo_wrapper在命令行上指定.但是我也不能foo在命令行上指定它以及它调用的任何函数.我foo_wrapper可以指定的原因是它允许我编写一个callEverything()调用的函数,只调用我想要的所有函数,然后将该符号放在命令行上.
我正在使用在 python 进程之间multiprocessing.Queue传递 numpy 数组float64。这工作正常,但我担心它可能没有达到应有的效率。
根据 的文档multiprocessing,放置在 上的对象Queue将被腌制。调用picklenumpy 数组会产生数据的文本表示,因此空字节被 string 替换"\\x00"。
>>> pickle.dumps(numpy.zeros(10))
"cnumpy.core.multiarray\n_reconstruct\np0\n(cnumpy\nndarray\np1\n(I0\ntp2\nS'b'\np3\ntp4\nRp5\n(I1\n(I10\ntp6\ncnumpy\ndtype\np7\n(S'f8'\np8\nI0\nI1\ntp9\nRp10\n(I3\nS'<'\np11\nNNNI-1\nI-1\nI0\ntp12\nbI00\nS'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00'\np13\ntp14\nb."
我担心这意味着我的数组被昂贵地转换为原始大小的 4 倍,然后在另一个过程中转换回。
有没有办法以原始未更改的形式通过队列传递数据?
我知道共享内存,但如果这是正确的解决方案,我不确定如何在其上构建队列。
谢谢!
当我在 cpython 3.6 上运行它时,以下程序打印hello world一次,然后永远旋转。
作为旁注,取消注释该await asyncio.sleep(0)行会使其hello world每秒打印一次,这是可以理解的。
import asyncio
async def do_nothing():
# await asyncio.sleep(0)
pass
async def hog_the_event_loop():
while True:
await do_nothing()
async def timer_print():
while True:
print("hello world")
await asyncio.sleep(1)
loop = asyncio.get_event_loop()
loop.create_task(timer_print())
loop.create_task(hog_the_event_loop())
loop.run_forever()
Run Code Online (Sandbox Code Playgroud)
这种行为(打印hello world一次)对我来说很有意义,因为hog_the_event_loop从不阻塞,因此不需要暂停执行。我可以依赖这种行为吗?当该行await do_nothing()运行时,是否有可能不是进入do_nothing()协程,而是执行实际上 suspend 和 resume timer_print(),导致程序hello world第二次打印?
更一般地说:python 什么时候会暂停一个协程的执行并切换到另一个协程?是不是有可能在任何使用的await关键字?还是仅在导致底层select调用(例如 I/O、睡眠定时器等)的情况下?
我知道如果hog_the_event_loop看起来像这样,它肯定不会让执行给另一个协程:
async …Run Code Online (Sandbox Code Playgroud) 我很想了解尾随auto&&返回类型究竟是什么意思,特别是区别于decltype(auto),在这里不起作用,以及未指定的返回类型,它也不起作用。
在下面的代码中,fn返回x_参数的字段。当参数是左值时,x_作为左值返回,等等。
在 的示例中fn_bad[123],int即使提供了左值参数,它似乎也会返回。我可以理解为什么-> auto会导致这种情况,但我希望-> decltype(auto)返回int&。为什么只-> auto&&工作?
#include <utility>
struct Foo { int x_; };
int main() {
auto fn_bad1 = [](auto&& foo) -> decltype(auto) { return std::forward<decltype(foo)>(foo).x_; };
auto fn_bad2 = [](auto&& foo) -> auto { return std::forward<decltype(foo)>(foo).x_; };
auto fn_bad3 = [](auto&& foo) { return std::forward<decltype(foo)>(foo).x_; };
auto fn = [](auto&& foo) -> auto&& { return …Run Code Online (Sandbox Code Playgroud)