Help doing simple threading

David Lees deblNonOspammy at theworld.com
Tue Mar 13 23:53:09 EST 2001


I am trying to learn how to use threads.  I want multiple threads to be
able to communicate.  I thought that threads started by a common process
would share globals in that module.  I want thread to be able to store
results in a common array.  However the following returns all zeros, so
clearly I am not passing anything back to the top level list 'z'.  Can
someone tell me how I should be passing things back from threads?

Thanks in advance.

David Lees
---------------------------------------

import thread

#Create a top level list of length n
def create_len(x,n):
    for i in range(n):
        x.extend([0])
          

def save_stuff(k):
    z[k]=k*k

n=10
global z
z=[]
create_len(z,n)

for i in range(n):
    thread.start_new(save_stuff,(i,))

for i in range(n):
    print z[i]



More information about the Python-list mailing list