no parallel threading?

Rich Harkins rharkins at thinkronize.com
Wed Sep 5 15:20:14 EDT 2001


Looking at the code I think the problem is with the call to the Thread
constructor.  Here's what's written:

test_thread1 = threading.Thread(None,test_thread_func1())

This will call test_thread_func1() first and pass the result to
threading.Thread.  What I *think* you want is:

test_thread1 = threading.Thread(None,test_thread_func1)	# No () here.

This passes a function reference to Thread which makes the call when the
thread has started.  You will also find that the function is not called
until you:

test_thread1.start()



Hope this helps...
Rich


> -----Original Message-----
> From: python-list-admin at python.org
> [mailto:python-list-admin at python.org]On Behalf Of Tobias Unruh
> Sent: Wednesday, September 05, 2001 1:37 PM
> To: python-list at python.org
> Subject: Re: no parallel threading?
>
>
> > On Wed, 5 Sep 2001, Tobias Unruh wrote:
> >
> > >Hi,
> > >
> > >I tried to execute two threads in parallel (linux 2.4.9, python 2.0):
> > >
> > >import time
> > >import threading
> > >
> > >def test_thread_func1():
> > >    print "Hallo1"
> > >    time.sleep(3.0)
> > >    print "Hallo1"
> > >
> > >def test_thread_func2():
> > >    print "Hallo2"
> > >    time.sleep(3.0)
> > >    print "Hallo2"
> > >
> > >printlock = threading.Lock()
> > >
> > >test_thread1 = threading.Thread(None,test_thread_func1())
> > >test_thread2 = threading.Thread(None,test_thread_func2())
> >
> > >Why this result? From the first four lines I concluded that the threads
> > >are executed one after the other (which is not intended). In this case,
> > >however, the number of active threads should be 1 in any case.
> But these
> > >values are ok. Is there something wrong with the output?
> > >sys.stdout.flush() does not help.
> >
> > Your computer is too fast. Try cycling in test_funcs and use random
> > sleep times (I even think, that you need delay loops: I am not sure
> > what happens when you issue sleep. Probabbly the whole process sleeps.
> > Then (probably) you will get intermingled strings.
>
> Hm, I tried it with
>
> def test_thread_func1():
>     k=0L
>     print "hello1"
>     for i in range(1000000):
>         k=k+1
>     print "hello1"
>
> but nothing changed.
>
> I installed now Python 2.2.a2. But again the same problem...
>
> Do I have it to write in C++?
>
> >
> > BTW, this is what intrigued Linus most and was supported from the very
> > first version of Linux ;-) (in processes, of course, not threads)
> >
> >
> > Sincerely yours, Roman Suzi
> > --
> > _/ Russia _/ Karelia _/ Petrozavodsk _/ rnd at onego.ru _/
> > _/ Wednesday, September 05, 2001 _/ Powered by Linux RedHat 6.2 _/
> > _/ "... Clinton sandwich: $5 of baloney and $20 in taxes" _/
> >
> >
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>





More information about the Python-list mailing list