0

I'm trying to use Process Arrays in order to use an Array structure across multiple Processes, but cant seem to initialize the example:

from multiprocessing import Process, Lock
from multiprocessing.sharedctypes import Value, Array
from ctypes import Structure, c_double,c_char_p

class Point(Structure):
    _fields_ = [('x', c_double), ('y', c_double)]

class Call(Structure):
    _fields_ = [('participantId',c_char_p)]

def modify(n,s):
    n.value **= 2
    #x.value **= 2
    s.value = s.value.upper()
    #for a in A:
    #    a.x **= 2
    #    a.y **= 2

if __name__ == '__main__':
    lock = Lock()

    n = Value('i', 7)
   #x = Value(c_double, 1.0/3.0, lock=False)
    s = Array('c', 'hello world', lock=lock)
    A = Array(Call,['ebtirgnm-sqr7-h2fd-wa80-baoctu684qma'], lock=lock)
   #A = Array(Point, [(1.875,-6.25), (-5.75,2.0), (2.375,9.5)], lock=lock)

    p = Process(target=modify, args=(n,s))
    p.start()
    p.join()

    print n.value
  #  print x.value
    print s.value

I get an error when initializing my Array with Call Object in this line:

A = Array(Call,['ebtirgnm-sqr7-h2fd-wa80-baoctu684qma',], lock=lock)


/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python /Users/gogasca/Documents/OpenSource/Development/Python/tpsEmulator/tpsEmulator/tools/Array.py
Traceback (most recent call last):
  File "/Users/gogasca/Documents/OpenSource/Development/Python/tpsEmulator/tpsEmulator/tools/Array.py", line 25, in <module>
    A = Array(Call,['1'], lock=lock)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/sharedctypes.py", line 115, in Array
    obj = RawArray(typecode_or_type, size_or_initializer)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/sharedctypes.py", line 89, in RawArray
    result.__init__(*size_or_initializer)
TypeError: expected Call instance, got str

I have tried:

  call1 = Call(["1"])
  ['1',]
  [('1')]
  [0]

No luck

1 Answer 1

2

There is an error here:

A = Array(Call,['ebtirgnm-sqr7-h2fd-wa80-baoctu684qma'], lock=lock)

Python is interpreting string as object, not arguments. It should be:

A = Array(Call,[('ebtirgnm-sqr7-h2fd-wa80-baoctu684qma',)], lock=lock)

Then python will interpret this as arguments. Same error is in examples:

call1 = Call(["1"])
['1',]
[('1')]
[0]

Let's trying to see what python thinks about last comma:

>>> print [('1')]
['1']
>>> print [('1',)]
[('1',)]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.