我阅读了subprocess提供的函数 - call,check_call,check_output,并了解每个函数的工作方式和功能之间的差异.我目前正在使用check_output,所以我可以访问stdout,并使用"try block"来捕获异常,如下所示:
# "cmnd" is a string that contains the command along with it's arguments.
try:
cmnd_output = check_output(cmnd, stderr=STDOUT, shell=True, timeout=3, universal_newlines=True);
except CalledProcessError:
print("Status : FAIL")
print("Output: \n{}\n".format(cmnd_output))
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是抛出异常时,"cmnd_output"未初始化并且无法访问stderr,我收到以下错误消息:
print("Output: \n{}\n".format(cmnd_output))
UnboundLocalError: local variable 'cmnd_output' referenced before assignment
Run Code Online (Sandbox Code Playgroud)
我认为那是因为异常导致"check_output"立即保释而没有任何进一步的处理,也就是在try块中分配给"cmnd_output".如果我错了,请纠正我.
有没有什么方法可以访问stderr(如果它被发送到stout就没关系)并且可以访问退出代码.我可以根据退出代码手动检查通过/失败,但不会发生异常.
艾哈迈德,谢谢你.
我设置了CentOS 6.3设置,其中控制台loglevel设置为4,默认日志级别设置为4.我知道我可以使用以下步骤更改默认控制台日志级别:
cat /proc/sys/kernel/printk
4 4 1 7
echo 5 > /proc/sys/kernel/printk
cat /proc/sys/kernel/printk
5 4 1 7
Run Code Online (Sandbox Code Playgroud)
但是,重新启动后,控制台日志级别将恢复为原始值.我是否需要重新编译内核,或者有一种方法可以让更改后的值在重新引导时保持持久性.
使用zip()函数时,我发现了一个奇怪的行为.当我执行以下操作len(list(z)),其中z是一个zip对象,结果为0(这对我来说似乎不对),并且该操作似乎清除了zip对象.有人可以帮我理解发生了什么.
# python3
Python 3.2.3 (default, Sep 30 2012, 16:41:36)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> w = [11, 22, 33, 44, 55, 66]
>>> x = [1, 2, 3, 4]
>>> y = ['a', 'b', 'c']
>>> z = zip(x, y, w)
>>> z
<zip object at 0x7f854f613cb0>
>>> list(z)
[(1, 'a', 11), (2, 'b', 22), (3, 'c', 33)]
>>> len(list(z))
0
>>> list(z)
[]
>>> z
<zip object at 0x7f854f613cb0> …Run Code Online (Sandbox Code Playgroud) 我在.h文件中定义了以下三个类:
class Base
{ // number of pure virtual functions };
class Derived1 : public Base
{ // number of pure virtual functions };
class Derived2 : public Base
{ // number of pure virtual functions };
Run Code Online (Sandbox Code Playgroud)
我希望此头文件的用户只能继承Derived1或Derived2.我想阻止用户继承Base.我"可以"为类Base使用关键字"final",但这会阻止我在头文件中进行子类化.我需要在头文件中包含所有上述类,因为用户需要提供Base和DerivedX类中方法的定义.
我正在考虑将继承范围限制在头文件中(类似于静态变量).任何建议或想法将不胜感激.
是否有ps(或类似工具)显示pthread名称的方法?我写了以下简单的程序:
// th_name.c
#include <stdio.h>
#include <pthread.h>
void * f1() {
printf("f1 : Starting sleep\n");
sleep(30);
printf("f1 : Done sleep\n");
}
int main() {
pthread_t f1_thread;
pthread_create(&f1_thread, NULL, f1, NULL);
pthread_setname_np(f1_thread, "f1_thread");
printf("Main : Starting sleep\n");
sleep(40);
printf("Main : Done sleep\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
是否有一个命令/实用程序(如ps)可用于显示上述程序的线程及其名称.
$ /tmp/th_name > /dev/null &
[3] 2055
$ ps -eLf | egrep "th_name|UID"
UID PID PPID LWP C NLWP STIME TTY TIME CMD
aal 31088 29342 31088 0 2 10:01 pts/4 …Run Code Online (Sandbox Code Playgroud) 在Java中,标准是定义toString()返回类的字符串表示的方法.除了重载operator<<,C++中是否有这样的标准?我知道有一些std::to_string()方法可以获得数字的字符串表示.C++标准是否谈到定义to_string()为类提供类似目的的方法,或者C++程序员是否遵循通用的做法?
我的系统是CentOS 6.3(运行内核版本2.6.32-279.el6.x86_64).
我有一个可加载的内核模块,它是一个管理PCIe卡的驱动程序.如果我insmod在操作系统启动并运行时手动插入驱动程序,则驱动程序会成功加载并运行.
不过,如果我尝试使用rpm安装了驱动程序,然后重新启动系统,启动操作系统卡住吐出了所有的CPU内核下面的"软锁"消息,除了一个核心是在"软锁"期间我的驱动程序创建的其中一个线程.
BUG: soft lockup - CPU#X stuck for 67s! [migration/8:36]
.......(same above message for all cores except one)
BUG: soft lockup - CPU#10 stuck for 67s! [mydriver_thread/8:36]
(one core is locked up in one of the threads in my driver).
Run Code Online (Sandbox Code Playgroud)
我在网上找了很多关于这个内核msg/bug的信息,并且有很多关于它的帖子,没有关于它的原因或如何调试.任何有关以下问题的帮助将非常感激:
我无法登录系统,我认为这是因为所有核心都处于"软锁定"状态,因此无法从shell提示符触发内核转储.我启用了SysRq,并尝试使用SysRq键组合触发内核转储,但没有运气.系统似乎没有响应键盘(甚至没有响应CapsLock按钮).关于如何在这种情况下触发内核转储的任何建议?
我可以想象我的驱动程序线程可能导致"软锁定".但是,由于我的驱动程序,"迁移"线程(内核线程)如何处于"软锁定"?
从浏览网络,"迁移"线程用于将任务从一个CPU移动到另一个CPU.有人可以帮我理解这个帖子究竟是做什么的吗?以及它如何受其他线程的影响,如果有的话.
我正在使用boost迭代器"recursive_directory_iterator"来递归扫描目录.但是,当迭代器运行到我的应用程序无法访问的目录时,抛出类型"boost :: filesystem3 :: filesystem_error"的异常,这会停止迭代器并且程序将中止.无论如何,我可以指示迭代器跳过这些目录.
我尝试使用boost :: filesystem遍历目录时建议的代码而不抛出异常 但是,它确实对我没用.我正在使用boost版本1.49.
遵循建议(我能想出的最好的)之后我的代码如下:
void scand()
{
boost::system::error_code ec, no_err;
// Read dir contents recurs
for (recursive_directory_iterator end, _path("/tmp", ec);
_path != end; _path.increment(ec)) {
if (ec != no_err) {
_path.pop();
continue;
}
cout << _path->path() << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
艾哈迈德,谢谢你.
我在使用map类时遇到了编译器问题,并编写了以下简单程序来突出显示错误:
1 #include <string>
2 #include <map>
3
4 using namespace std;
5
6 int main()
7 {
8 map<string, string> testmap;
9
10
11 testmap["one"] = 11;
12 testmap["two"] = 22;
13 testmap["zero"] = 0;
14 // testmap["zero"] = 10;
15
16 return 0;
17 }
Run Code Online (Sandbox Code Playgroud)
我收到以下编译错误:
g ++ ./test.cc ./test.cc:在函数'int main()'中:./ test.cc:13:23:错误:'testmap.std :: map <_Key中'operator ='的模糊重载,_Tp,_Compare,_Alloc> :: operator [],std :: basic_string,std :: less>,> std :: allocator,std :: basic_string >>>((*&std :: basic_string(((const char)*)"零"),((const std :: allocator)(&std :: …
我使用的是Python 2.4.我想打印一个左对齐但带有"偏移"的字符串.我的意思是,在它之前打印一个带有一定数量空格的字符串.
例:
在宽度为20的空格中打印字符串"Hello",左对齐,但在字符串之前插入五个空格.
" Hello " #(The string has 5 spaces prior, and 10 space after)
print "Hello".ljust(20) #does not cut it.
Run Code Online (Sandbox Code Playgroud)
我可以使用以下作为解决方法:
print " ", "Hello".ljust(15)
Run Code Online (Sandbox Code Playgroud)
有没有比打印5个字符串更好的方法.
艾哈迈德,谢谢你.
c++ ×4
c++11 ×4
linux ×3
python ×3
linux-kernel ×2
python-3.x ×2
boost ×1
centos ×1
driver ×1
inheritance ×1
ps ×1
pthreads ×1
stl ×1
string ×1