Help doing simple threading 
Author Message
 Help doing simple threading

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]



Sun, 31 Aug 2003 12:53:09 GMT  
 Help doing simple threading


Quote:

>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?

Um.  No offense, but I think you'd find it more productive if you learn
a bit more about python before trying to use threads.  For example,
"global" has no effect at the module level.
--

Androgynous poly {*filter*} vanilla {*filter*} het Pythonista   http://www.*-*-*.com/
Hugs and backrubs -- I break Rule 6

Python:
    Programming the way
    Guido indented it



Sun, 31 Aug 2003 13:10:37 GMT  
 Help doing simple threading

Quote:
>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
>esults 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?

Your code has several problems:

a) You don't synchronize your main thread with the threads you start.
    That means your main thread continues running BEFORE the
    started threads make any work.

b) Modifying shared resources within threads is dangerous. Think
   about using locks to get rid of this problem.

c) Try the threading module. It gives you a better control and programmer
    interface to work with thread in Python.

----

Digital Creations - www.digicool.com, www.zope.org



Sun, 31 Aug 2003 20:54:12 GMT  
 Help doing simple threading
Thanks, these comment were quite helpful.  The most serious error was
'a)'.  when I just put the main to sleep after starting up the threads
it worked fine.  The problem was that I was printing out the array
values, before all the threads finished executing.

David

Quote:

<snip>

> Your code has several problems:

> a) You don't synchronize your main thread with the threads you start.
>     That means your main thread continues running BEFORE the
>     started threads make any work.

> b) Modifying shared resources within threads is dangerous. Think
>    about using locks to get rid of this problem.

> c) Try the threading module. It gives you a better control and programmer
>     interface to work with thread in Python.

> ----

> Digital Creations - www.digicool.com, www.zope.org



Mon, 01 Sep 2003 12:14:51 GMT  
 Help doing simple threading

Quote:

> Thanks, these comment were quite helpful.  The most serious error was
> 'a)'.  when I just put the main to sleep after starting up the threads
> it worked fine.  The problem was that I was printing out the array
> values, before all the threads finished executing.

If by sleep, you just mean you added something like time.sleep() with
a fixed value, then you're still at risk, depending on the performance
of the other thread executions.

You will need to use some sort of synchronization primitive to ensure
that the main thread waits long enough for each of the worker threads
to complete.  If you know you want to wait until the threads actually
complete execution and terminate, and if you use the higher level
Threading module, you can "join" on the threads to block until they
complete.

--
-- David
--
/-----------------------------------------------------------------------\

  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 C{*filter*}Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



Mon, 01 Sep 2003 12:28:00 GMT  
 Help doing simple threading



Quote:
> Thanks, these comment were quite helpful.  The most serious error was
> 'a)'.  when I just put the main to sleep after starting up the threads
> it worked fine.  The problem was that I was printing out the array
> values, before all the threads finished executing.

Using sleep() is also dangerous. This is not the way to synchronize threads.
The threading module offers you several possibilites (e.g. join() method)
for
a clean, working solution.

Andreas



Mon, 01 Sep 2003 21:25:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Novice Programmer needs help doing something simple, MERGE

2. ARexx: doing simple interactive graphics?

3. Simple Newbie Question: What am I doing wrong?

4. Doing the simplest thing with Tkinter Canvas

5. ANNC: Tcl/Tk 8.4.1 done for threads for windows (binaries)

6. Pausing until threads are done

7. Simple task-not simple for Dummy, Help

8. Help on doing help

9. simple thread

10. simple threading with gcc

11. Simple Ruby-Thread question.

12. Simple Thread Question

 

 
Powered by phpBB® Forum Software