小编Ros*_*s W的帖子

是boost :: property_tree :: ptree线程安全吗?

我在一段代码中的几个线程中使用了boost read_json.简化的呼叫细分如下.我在其中一个线程(有时是另一个)中得到段错误,这让我觉得read_json不是线程安全的(或者我只是以愚蠢的方式使用它)

void someclass::dojson() {
   using boost::property_tree::ptree;
   ptree pt;
   std::stringstream ss(json_data_string);

   read_json(ss,pt);
 }
Run Code Online (Sandbox Code Playgroud)

现在json_data_string在两个类之间是不同的(它只是通过套接字接收的json数据).

那么read_json线程是安全的还是我必须互斥它(而不是)或者是否有更好的方法来调用线程安全的read_json?

c++ json boost thread-safety segmentation-fault

18
推荐指数
2
解决办法
5850
查看次数

我如何从双端队列中弹出()大量元素?

我有一个deque对象,它包含大量数据.我想从队列的前面提取4096个元素(我将它用作一种FIFO).似乎应该有这样做的方式,而不必迭代超过4096个pop请求.

这是正确/有效/愚蠢的吗?

A = arange(100000)
B = deque()
C = [] # List will do 
B.extend(A) # Nice large deque

# extract 4096 elements
for i in xrange(4096):
   C.append(A.popleft())
Run Code Online (Sandbox Code Playgroud)

python deque

18
推荐指数
2
解决办法
6848
查看次数

如何在Python中逐字打印字符串

这可能很简单,但我找不到它.

我需要打印Python中包含的字符串.我正在从串口收集数据,我需要知道它是否正在发送CR或CRLF +其他不是ascii的控制代码.

举个例子说我有

s = "ttaassdd\n\rssleeroo"
Run Code Online (Sandbox Code Playgroud)

那我想做的是:

print s
Run Code Online (Sandbox Code Playgroud)

它会显示\n\r而不是将它们转换为转义字符.

python ansi-escape

14
推荐指数
1
解决办法
1万
查看次数

记录在文档字符串中作为参数传递的字典

相关内容:为 dicts 格式化 python 文档字符串

为作为参数传递给函数的字典提供文档的正确方法是什么?这是我编写的样式示例(基于 Sphinx 的 Google 文档样式):

def class_function_w_dict_argument(self, T_in, c_temps):
    """ This calculates things with temperatures

    Notes:
        All temperatures should be given in Kelvin

    Args:
        T_in (float): Known temperature (K)
        c_temps (dict): Dictionary of component temperatures
            {T1 (float): temperature (K)
             T2 (float): temperature (K)
             T3 (float): temperature (K)
             T4 (float): temperature (K)
             T5 (float): temperature (K)}

    Returns:
        T_out (float): Calculated temperature
    """
Run Code Online (Sandbox Code Playgroud)

python dictionary python-sphinx

10
推荐指数
1
解决办法
2467
查看次数

boost:只需迭代ptree的元素

这应该很简单(我只是学习提升,所以我错过了一些东西)

我已经使用json_read读了一些简单的JSON,现在有了一个ptree.Web上的所有示例都使用ptree.get("entry_name")来获取条目.我想做的就是:

 ptree pt;
 read_json(ss,pt);

 BOOST_FOREACH(ptree::value_type &v, pt)
 {
   std::cout << v.{entry_name} << v.{value}
 }
Run Code Online (Sandbox Code Playgroud)

即循环遍历ptree并写出每个名称(即你放入pt.get()的内容)和它的相应值.

对不起,如果这很简单

罗斯

loops boost-propertytree

9
推荐指数
2
解决办法
1万
查看次数

使用__del__停止python线程

我在Python中有一个线程程序工作正常,但是一旦线程运行就不会调用__del__:

class tt(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.stop_event = threading.Event()

    def __del__(self):
        print "Deleting"
        self.stop_event.set()
        time.sleep(5)

    def run(self):
        self.stop_event.clear()
        while not self.stop_event.isSet():
             self.do_stuff()
        threading.Thread.__init__(self)

    def stop(self):
        self.stop_event.set()
Run Code Online (Sandbox Code Playgroud)

例如,如果我这样做

tc = aa()
del tc 
Run Code Online (Sandbox Code Playgroud)

它工作正常(我得到删除消息暂停等).但是,如果我这样做:

tc = aa()
tc.start()
del tc
Run Code Online (Sandbox Code Playgroud)

然后__del__没有运行(注意当我执行tc.start(); tc.stop(); del tc时它会执行__del__.

我在ipython中运行它

python multithreading

5
推荐指数
1
解决办法
4338
查看次数

位移数字输出错误的输出`std :: cout`

unsigned int command = 4;
cout << command;

command = (command << 1);
cout << command;

command = (command << 1);
cout << command;
Run Code Online (Sandbox Code Playgroud)

输出:

4
8
10
Run Code Online (Sandbox Code Playgroud)

为什么是最后一行的输出10,而不是16

c++ bit-shift

0
推荐指数
1
解决办法
283
查看次数