list - Can't return appended value from within a Python process -


i running python 2.6.5 on ubuntu 8.10.

for project working on, need run several processes concurrently , save each of outputs own respective lists. since can't return output value directly process, passing output list argument target function , appending output list. problem when try access list after running process, list still empty. simplified version of problem i'm having below along error message.

code:

from multiprocessing import process import sys, math, os,commands  outputs = []  def calculate(a,b,outputs):     c = a*b     outputs.append(c)     #return c  outputs1 = []  p1 = process(target = calculate, args = (2,3,outputs1))  p1.start() p1.join()  print 'len(outputs1) = ' + str(len(outputs1))  print 'outputs1 = ' + str(outputs1[0]) 

error:

len(outputs1) = 0 traceback (most recent call last):   file "append_test.py", line 23, in <module>     print 'outputs1 = ' + str(outputs1[0]) indexerror: list index out of range 

i trying keep each process independent of others fear of corrupting data. i've looked trying use array module multiprocessing, appears append specific lists. when run exact same code thread instead of process, desired output no problem, leads me believe issue of memory sharing.

when use separate processes, each process gets own copy of in memory. that's why parent process never sees in outputs: each child process appending own copy of outputs.

you need use form of interprocess communication. python's multiprocessing library supplies 2 features this: pipes , queues.

for example, using queue:

>>> multiprocessing import process, queue >>> def f(q): q.put("hello child process") ...  >>> q = queue() >>> p = process(target=f, args=(q,)) >>> p.start() >>> p.join() >>> q.get() 'hello child process' 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -