2013-12-09 41 views
0
>>> from multiprocessing import Array, Value 
>>> import numpy as np 
>>> a = [(i,[]) for i in range(3)] 
>>> a 
[(0, []), (1, []), (2, [])] 
>>> a[0][1].extend(np.array([1,2,3])) 
>>> a[1][1].extend(np.array([4,5])) 
>>> a[2][1].extend(np.array([6,7,8])) 
>>> a 
[(0, [1, 2, 3]), (1, [4, 5]), (2, [6, 7, 8])] 

繼蟒多處理example: def test_sharedvalues():我嘗試使用下面的代碼創建一個共享的代理對象:類型錯誤而代表任意元素類型multiprocessing.Array

shared_a = [multiprocessing.Array(id, e) for id, e in a] 

,但它給我一個錯誤

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/lib64/python2.6/multiprocessing/__init__.py", line 255, in Array 
    return Array(typecode_or_type, size_or_initializer, **kwds) 
    File "/usr/lib64/python2.6/multiprocessing/sharedctypes.py", line 87, in Array 
    obj = RawArray(typecode_or_type, size_or_initializer) 
    File "/usr/lib64/python2.6/multiprocessing/sharedctypes.py", line 60, in RawArray 
    result = _new_value(type_) 
    File "/usr/lib64/python2.6/multiprocessing/sharedctypes.py", line 36, in _new_value 
    size = ctypes.sizeof(type_) 
TypeError: this type has no size 

回答

0

好的。問題是解決了

我改變

>>> a = [(i,[]) for i in range(3)] 

>>> a = [('i',[]) for i in range(3)] 

,這解決了類型錯誤。

事實上,我也發現,我並不一定必須使用i設定爲在範圍內計數(3)(因爲陣列自動允許分度),則「i」是爲下multiprocessing.sharedctypes c_int的類型代碼

希望這有助於。