小编oyj*_*yjh的帖子

Pickler中__reduce__的确切用法是什么?

我知道,为了被picklable,类必须覆盖__reduce__方法,并且必须返回字符串或元组.

这个功能如何运作?具体用途是__reduce__什么?它何时被使用?

pickle python-2.7

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

google.protobuf.Any 和 google.protobuf.Value 有什么区别?

我想将 int/int64/double/float/uint32/uint64 序列化为 protobuf,我应该使用哪一个?哪一种更有效?

例如 :

message Test {
    google.protobuf.Any   any   = 1;   // solution 1
    google.protobuf.Value value = 2;   // solution 2
};

message Test {                         // solution 3
   oneof Data {
        uint32   int_value    = 1;
        double   double_value = 2;
        bytes    string_value = 3;
        ...
   };
};
Run Code Online (Sandbox Code Playgroud)

protocol-buffers protobuf-c

7
推荐指数
1
解决办法
2922
查看次数

哪一个是数据描述符和非数据描述符的正确定义?

它们都是来自文档的python:

第一个说:

如果一个对象同时定义__get__()__set__(),它被认为是一个数据描述符.仅定义__get__()的描述符称为非数据描述符(它们通常用于方法,但其他用途也是可能的).

第二个说:

如果描述符定义__set__()和/或__delete__(),则它是数据描述符; 如果它既不定义,则它是非数据描述符.通常情况下,数据描述符同时定义__get__()__set__(),非数据描述符刚才的__get__()方法.

问题是:仅仅定义__set__生成数据描述符是否足够?

我们参考python源代码,我发现了这个:

#define PyDescr_IsData(d) (Py_TYPE(d)->tp_descr_set != NULL)
Run Code Online (Sandbox Code Playgroud)

似乎我们只能定义__set__没有__get__.

然后我转而写一些例子来证明我得到了什么:

class GetSet(object):
def __get__(self, instance, cls =None):
    print('__get__')

def __set__(self, obj, val):
    print('__set__')

class Get(object):
    def __get__(self, instance, cls =None):
        print('__get__')

class Set(object):
    def __set__(self, obj, val):
        print('__set__')

class UserClass(object):
    a = Get()
    b = Set()
    c = GetSet()


u = …
Run Code Online (Sandbox Code Playgroud)

python python-3.4

4
推荐指数
1
解决办法
204
查看次数

是否可以为 redis 中的所有键设置默认 ttl?

我已经阅读了 redis 配置文档,但找不到这样的选项。

我搜索并发现“默认情况下,密钥将永远存在”。我想急切地改变这种默认行为。

Normally Redis keys are created without an associated time to live. The key will simply live forever, unless it is removed by the user in an explicit way, for instance using the DEL command. 
The EXPIRE family of commands is able to associate an expire to a given key, at the cost of some additional memory used by the key. When a key has an expire set, Redis will make sure to remove the …
Run Code Online (Sandbox Code Playgroud)

redis hiredis

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

为什么在这种情况下shared_ptr对象在没有锁的情况下工作得很好?

一个读取器线程和多个写入器线程同时访问shared_ptr对象并且它运行良好,代码如下(但是,如果我将写入行代码从“=”修改为“重置”,它将在读取时进行coredump):

shared_ptr.reset 意味着 coredump ,“operator =”意味着效果很好?(我试了100多次)

bool start = false;
std::mutex mtx;
std::condition_variable cond;
std::shared_ptr<std::string> string_ptr(new std::string("hello"));

int main() {
  auto read = []() {
    {
      std::cout << "readddd" << std::endl;
      std::unique_lock<std::mutex> lck(mtx);
      while (!start) {
        cond.wait(lck);
      }
    }
    for (int i = 0; i < 100000; ++i) {
      std::cout << *string_ptr.get() << std::endl;
    }
  };

  auto write = []() {
    {
      std::unique_lock<std::mutex> lck(mtx);
      while (!start) {
        cond.wait(lck);
      }
    }
    for (int i = 0; i < 100000; ++i) …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading shared-ptr c++11

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