Thread confusion?

Lyle Johnson ljohnson at resgen.com
Wed May 23 18:19:39 EDT 2001


> I'm having a problem with python threading, where python is probably
> doing EXACTLY as it was designed, but it's not what I would expect.
>
> Example:
>
>       def   run_a_thread ( value1, value2):
>          d1 = run_sub_function1 (value1)
>          d2 =run_sub_function2 (value2)
>       return d1, d2
>
>    data = start_new_thread (run_a_thread, (v1, v2,) )
>
> Now the thread appears to start, but as far as I can tell, it runs for
> "no time", and returns value of NONE.

thread.start_new_thread() always returns None, so the return value of your
function is irrelevant. If you want to pass data back to the main thread
you'd need to either modify the input arguments (value1, value2) themselves
or use global data.

> As I've read, the thread continues until the function that is run is
> exited.... But run_a_thread isn't exited until after run_sub_function2
> is finished (*In my Logical universe that is*)
>
> I'm assuming as soon as run_a_thread runs the "run_sub_function1"
> module, run_a_thread is considered "finished", and the thread closes...

No, actually the first case should be true -- both run_sub_function1() and
run_sub_function2() should complete before run_a_thread() exits and the
thread terminates. But you may have assumed that run_sub_function2() didn't
run because you didn't get the return value you expected. If in doubt you
could always use the classic debugging technique of adding print statements
to see what's happening ;)






More information about the Python-list mailing list