小编pap*_*ane的帖子

为什么在共享带有字符串的ctypes.Structure而不仅仅是字符串时,子进程的内存使用(python多处理)进程如此不同?

以下代码使用multiprocessing's Array跨进程共享大量unicode字符串.如果我使用c_wchar_p类型,子进程的内存使用量大约是父进程中使用的内存的四分之一(如果我更改数组中的条目数量,则数量会发生变化).

但是,如果我使用ctypes.Structure单个c_wchar_p字段,则子进程的内存使用量是常量且非常低,而父进程的内存使用量则翻倍.

import ctypes
import multiprocessing
import random
import resource
import time

a = None

class Record(ctypes.Structure):
    _fields_ = [('value', ctypes.c_wchar_p)]
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return '(%s)' % (self.value,)

def child(i):
    while True:
        print "%ik memory used in child %i: %s" % (resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024, i, a[i])
        time.sleep(1)
        for j in xrange(len(a)):
            c = a[j]

def main():
    global a
    # uncomment this line and comment the …
Run Code Online (Sandbox Code Playgroud)

python memory shared-memory multiprocessing

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

在特定情况下,为什么Python使用百分比运算符同时支持元组和字典格式?

此输入:

'%s %(?)s' % {'?': 'a'}
Run Code Online (Sandbox Code Playgroud)

结果如下:

"{'?': 'a'} a"
Run Code Online (Sandbox Code Playgroud)

它将源字符串中的基于元组和基于dict的占位符替换为如果单独使用它们中的任何一个,您将期望的值。

但是,如果反转字符串中占位符的位置,则会出现TypeError:

In [2]: '%(?)s %s' % {'?': 'a'}
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-7989b5f88c97> in <module>()
----> 1 '%(?)s %s' % {'?': 'a'}

TypeError: not enough arguments for format 
Run Code Online (Sandbox Code Playgroud)

python dictionary tuples string-formatting

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

运行Crate时ElasticSearch API在哪里暴露?

我已成功在crate上安装了elasticsearch head插件,可以访问其Web UI,但无法连接.我希望能够使用它来可视化底层elasticsearch商店中的数据.有没有办法直接访问elasticsearch API,以便头部可以工作?

plugins elasticsearch crate

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