在子进程中的共享 c_wchar_p 中设置字符串的值? [英] Set value of string in a shared c_wchar_p in a subproccess?

查看:81
本文介绍了在子进程中的共享 c_wchar_p 中设置字符串的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到过这样的情况:

主进程生成一些子进程,他们应该将结果以字符串和数字类型写入共享对象中,对于数字类型没有问题,但对于字符串,值将丢失.

将多处理导入为 mpfrom ctypes import Structure, c_double, c_wchar_p, c_int# 共享对象类类 SharedObj(结构):_fields_ = [('name', c_wchar_p), ('val', c_double)]def run_mp( values , lock , s ) :对于 i 在范围内( s , len( values ) , 2 ):锁定.获取()values[i].name = str( i ) # 将字符串值写入共享 objvalues[i].val = float( i )打印(tmp:%d"% i)锁.释放()定义主():# 创建共享对象和互斥锁values = mp.Array( SharedObj , [SharedObj() for i in range( 10 )] )lock_j = mp.Lock()# 创建两个子进程形成函数run_mpp1 = mp.Process( target=run_mp , args=( values , lock_j , 0 ))p2 = mp.Process( target=run_mp , args=( values , lock_j , 1 ))p1.start()p2.start()p1.join()p2.join()对于 v 值:打印()打印(资源名称:%s"%v.name)打印(res val:%f"% v.val)如果 __name__ == '__main__':主要的()

结果包含 c_double 的共享对象中的字段被写入该字段,但在子进程 rum-mp 中生成的字符串 ( string values[i].name = str( i ) ) 将在主进程中丢失.

有没有办法保存子进程生成的字符串?

这段代码的输出如下所示:

主进程中产生的字符串是完全随机的.

tmp: 0时间:2时间:3时间:4资源名称:    羍    羍资源价值:0.000000资源名称:    羍    羍资源价值:1.000000资源名称:资源价值:2.000000 ....

解决方案

这是您的代码的稍微修改的版本:

<前>#!/usr/bin/env python将多处理导入为 mpdef run_mp( 值):对于 c_arr, c_double 的值:c_arr.value = '你好 foo'c_double.value = 3.14定义主():锁 = mp.Lock()child_feed = []对于范围内的我(10):child_feed.append((mp.Array('c', 15, lock = lock),mp.Value('d', 1.0/3.0, lock = lock)))p1 = mp.Process(target=run_mp, args=(child_feed,))p2 = mp.Process(target=run_mp, args=(child_feed,))p1.start()p2.start()p1.join()p2.join()对于 c_arr,child_feed 中的 c_double:打印()打印(资源名称:%s"% c_arr.value)打印(res val:%f"% c_double.value)如果 __name__ == '__main__':主要的()

看看http://docs.python.org/library/multiprocessing.html 有一个使用字符数组的例子.

还有允许共享内存的 mmap 模块 http://docs.python.org/library/mmap.html 但是有了这个,您必须通过信号量同步访问自己.如果您喜欢更简单的方法,请使用管道.

I've a situation like this:

The main process generate some sub-process that they should write the result in a shared object in string and numeric types, for the numeric types there's no problem but with the string the value will be lost.

import multiprocessing as mp
from ctypes import Structure, c_double, c_wchar_p, c_int

# shared obj class
class SharedObj(Structure):
    _fields_ = [('name', c_wchar_p), ('val', c_double) ]

def run_mp( values , lock , s ) :
    for i in range( s , len( values ) , 2 ):
        lock.acquire()
        values[i].name = str( i ) # write the string value in the shared obj
        values[i].val = float( i )
        print( "tmp: %d" % i )
        lock.release()

def main():
    # creating the shared obj and mutex
    values = mp.Array(  SharedObj , [SharedObj() for i in range( 10 )] )
    lock_j = mp.Lock()

    # creating two sub-process form the function run_mp
    p1 = mp.Process( target=run_mp , args=( values , lock_j , 0 ))
    p2 = mp.Process( target=run_mp , args=( values , lock_j , 1 ))

    p1.start()
    p2.start()

    p1.join()
    p2.join()

    for v in  values:
        print()
        print( "res name: %s" % v.name )
        print( "res val: %f " % v.val )


if __name__ == '__main__':
    main()

As a result the field in the shared object containing the c_double is written in the field, but the string generated in the sub-processes rum-mp ( string values[i].name = str( i ) ) will be lost in the main process.

There is a method for saving the strings generated in sub-process?

The output of this code looks like:

Where the resulting string in the main process are completely random.

tmp: 0
tmp: 2
tmp: 3
tmp: 4

res name: ����羍����羍
res val: 0.000000
res name: ����羍����羍
res val: 1.000000
res name:
res val: 2.000000   ....

解决方案

Here is a slightly modified version of your code:

#!/usr/bin/env python

import multiprocessing as mp


def run_mp( values ):
    for c_arr, c_double in values:
        c_arr.value = 'hello foo'
        c_double.value = 3.14

def main():
    lock = mp.Lock()
    child_feed = []
    for i in range(10):
        child_feed.append((
            mp.Array('c', 15, lock = lock),
            mp.Value('d', 1.0/3.0, lock = lock)
        ))

    p1 = mp.Process( target=run_mp , args=(child_feed,))
    p2 = mp.Process( target=run_mp , args=(child_feed,))

    p1.start()
    p2.start()

    p1.join()
    p2.join()

    for c_arr, c_double in child_feed:
        print()
        print( "res name: %s" % c_arr.value )
        print( "res val: %f " % c_double.value )


if __name__ == '__main__':
    main()

Take a look at http://docs.python.org/library/multiprocessing.html There is an example of using Array of chars.

There is also mmap module alowing to share memory http://docs.python.org/library/mmap.html but with this you have to Sync access yourself possibly by semaphores. If you like more simple approach just use pipes.

这篇关于在子进程中的共享 c_wchar_p 中设置字符串的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆