simple threading

Aurelio Martín amartin at wpsnetwork.com
Tue Oct 9 13:53:49 EDT 2001


Mark Robinson <m.1.robinson at herts.ac.uk> escribió en el mensaje de noticias
3BC32938.3080902 at herts.ac.uk...
> I am trying run a particularly processor hungry function in a
> seperate thread but I can't quite figure out how to return
> the arguments from the object now.
>
> previously the function call was coded as:
>
> motifs = motifFinder.findOtherOccurances(posList, doubles)
>
> the new code is:
>
> motifs = thread.start_new_thread(motifFinder.findOtherOccurances,\
> (posList, doubles))
>
> The function seems to run fine, but it always returns None,
> can anyone tell me what I am doing wrong?
>
> TIA
>
> Blobby
>

It's thread.start_new_thread who's returning None, not
motifFinder.findOtherOccurances (probably it hasn't ended its execution yet,
by the time thread.start_new_thread returns ). You have to wait for the
thread to end its execution, and then ask for the returned value.

eg

def thread_add( arg1, arg2, result ):
    # Start thread
    result[ 'ok' ] = 0
    # Calculate the result
    sum = arg1 + arg2
    # End thread
    result[ 'data' ] = sum
    result[ 'ok' ] = 1


result = { 'ok': 0 }
# Launch the thread
thread.start_new_thread( thread_add, ( 10, 5, result ) )
# Wait for thead to end
while not result[ 'ok' ]:
    DoSomethingElse()
# Looks like the thread is over
print result[ 'data' ]


Probably there are lots of better ways to do this ( signals, maybe ? ).






More information about the Python-list mailing list