我正在寻找从python调用shell脚本的方法,并使用日志记录将他们的stdout和stderr写入文件.这是我的代码:
import logging
import tempfile
import shlex
import os
def run_shell_command(command_line):
command_line_args = shlex.split(command_line)
logging.info('Subprocess: \"' + command_line + '\"')
process_succeeded = True
try:
process_output_filename = tempfile.mktemp(suffix = 'subprocess_tmp_file_')
process_output = open(process_output_filename, 'w')
command_line_process = subprocess.Popen(command_line_args,\
stdout = process_output,\
stderr = process_output)
command_line_process.wait()
process_output.close()
process_output = open(process_output_filename, 'r')
log_subprocess_output(process_output)
process_output.close()
os.remove(process_output_filename)
except:
exception = sys.exc_info()[1]
logging.info('Exception occured: ' + str(exception))
process_succeeded = False
if process_succeeded:
logging.info('Subprocess finished')
else:
logging.info('Subprocess failed')
return process_succeeded
Run Code Online (Sandbox Code Playgroud)
我确信无需创建临时文件来存储流程输出就可以实现.有任何想法吗?
钱德勒卡鲁斯在他的谈话对编译器的优化表示,编译器在与按引用传递参数优化功能可怕.我可以理解,当参数是非const引用时很难,因为编译器必须处理内存,或者参数的类型很复杂(一些奇怪的结构或类).但是如果参数是const引用而内置类型真的有问题吗?优化器可以代替const float&用const float?启用SSE指令后,它可能会更有帮助,因为编译器可以正确地为它们对齐数据.
是否可以在C++ 11/14中编写类似的内容?
#include <iostream>
#include <vector>
template <typename T>
T Get();
template <typename T>
struct Data {
std::vector<T> data;
};
template <>
template <typename T>
Data<T> Get<Data<T>>() {
return Data<T>{{T{}, T{}}};
}
template <>
template <typename T>
std::vector<T> Get<std::vector<T>>() {
return std::vector<T>(3);
}
int main() {
std::cout << Get<Data<int>>().data.size() << std::endl; // expected output is 2
std::cout << Get<std::vector<int>>().size() << std::endl; // expected output is 3
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,重载将无济于事,因为调用Get<...>()将是不明确的(请参阅参考资料):
template <typename T>
Data<T> Get() { …Run Code Online (Sandbox Code Playgroud) 在我的代码库中,我有无范围枚举,没有底层类型,如下所示:
enum EFoo {
EF_AAA = 0,
EF_UNKNOWN = 1,
EF_BBB = 2,
EF_MAX
}
Run Code Online (Sandbox Code Playgroud)
我想让它成为一个 protobuf 枚举,这样它就可以在其他 protobuf 消息中直接作为枚举重用,而不是作为某种int*字段。所以我想 .proto 文件中的枚举声明将如下所示:
enum EFoo {
EF_AAA = 0;
EF_UKNOWN = 1;
EF_BBB = 2;
}
Run Code Online (Sandbox Code Playgroud)
这是一个棘手的部分。随着时间的推移,可能会添加新字段,例如EF_CCC = 3,因此我无法EF_MAX像在 C++ 代码中那样进行声明,因为它将破坏与包含类型字段的序列化消息的二进制兼容性EFoo。并且EF_MAX在整个代码库的 API 中用作类型变量的未知值,EFoo并且EF_MAX永远不会序列化。然而,有一个EFoo_ARRAYSIZEof 类型int,其语义正是EF_MAX。所以我正在考虑将所有内容替换EF_MAX为EFoo_ARRAYSIZE,但有一件事困扰着我,它将需要在某些地方执行 astatic_cast<EFoo>(EFoo_ARRAYSIZE)以避免编译器警告,并且根据标准,它将被视为未定义的行为,这可能会导致令人讨厌的行为优化和错误。
我的问题是,我该如何解决我的问题?或者也许我错了,我的解决方案将全部替换EF_MAX为static_cast<EFoo>(EFoo_ARRAYSIZE)是安全的?
如果它可能很重要,我正在谈论 C++11 标准。
我一直在阅读有关杂乱无章的黑客和思想,编译器是否能够避免在以下代码中进行分支:
constexpr int min(const int lhs, const int rhs) noexcept {
if (lhs < rhs) {
return lhs;
}
return rhs
}
Run Code Online (Sandbox Code Playgroud)
用(解释)替换它:
constexpr int min(const int lhs, const int rhs) noexcept {
return rhs ^ ((lhs ^ rhs) & -(lhs < rhs));
}
Run Code Online (Sandbox Code Playgroud) 这是一段代码片段.
x = {}
x[1] = len(x)
print x
{1: 0}
Run Code Online (Sandbox Code Playgroud)
这个定义得很好吗?也就是说,可以x == {1: 1}呢?
因为我记得C++ '98中的等效程序(如果我们使用std::map)具有未定义的行为.使用VS编译器和G ++编译时,程序的输出是不同的.
这是一个代码片段:
#include <functional>
#include <iostream>
#include <memory>
template <typename T>
using Callback = std::function<void(const T)>;
template <typename T>
void Call(const T yyy, const Callback<T>& callback) {
callback(yyy);
}
template <typename T>
class MyCallback {
public:
explicit MyCallback(const T counter_init) : counter_{counter_init} {}
void operator ()(const T xxx) {
std::cout << counter_ << '\t' << xxx << std::endl;
++counter_;
}
private:
T counter_;
};
int main() {
const auto callback = std::make_unique<MyCallback<int>>(0);
Call(111, *callback); // expected output is "0 \t 111" …Run Code Online (Sandbox Code Playgroud) 我曾经多次听说编译器不会优化内联汇编,或者内联汇编对它来说是一个黑盒子.我很怀疑,因为我没有看到编译器失败的任何情况,我不在乎.
但今天我在GCC维基上找到了一个名为DontUseInlineAsm的页面.它包含了人们之前告诉我的相同问题,但没有详细说明为什么编译器不理解内联asm,因此不会优化它.那么,有没有人知道编译器不做这些优化的原因?
当然,我正在收拾特殊情况
asm volatile("" : : "g"(value) : "memory"); 要么
asm volatile("" : : : "memory");
当我们明确地告诉编译器这个代码有可见的副作用时,因此它不应该优化它.
c++ ×7
c++11 ×5
c++14 ×3
optimization ×2
python ×2
templates ×2
assembly ×1
enums ×1
logging ×1
python-2.7 ×1
refactoring ×1
stl ×1
subprocess ×1