returning a value from a thread
apardon at forel.vub.ac.be
Thu Jul 15 10:31:10 CEST 2004
Op 2004-07-14, Jeff Shannon schreef <jeff at ccvcorp.com>:
> Christopher T King wrote:
>>On Wed, 14 Jul 2004, Ken Godee wrote:
>>>I thought there was some sort of memory type container
>>>I could use...
>>>ie. store value in memory from module3 and be able to
>>>read it from module1.
>>>The closest I can think of is to pass a queue reference
>>>and stor values from module3 in the queue and then read them
>>>back while in module1.
>>Actually, any kind of container object would do: you could pass a list,
>>dictionary, or class to the thread, and the thread could store its results
>>A hackish method would be to pass the dictionary returned by locals() in
>>module1 to the thread. This way the thread could set a value in module1
>>Just don't forget to .join() on the thread in module1 before accessing the
>>container object! (Assuming you're using threading) I just wish Python's
>>.join() could return values, like pthread_join can.
> This may work if the worker thread will perform a relatively short task
> and then die *before* you access the result. But lists and dictionaries
> are not thread-safe -- if they are potentially accessed by multiple
> threads concurrently, then the behavior will be unpredictable.
I thought the GIL was supposed to take care of that.
> of a case where thread B starts to update a dictionary, inserts a key
> but is interrupted before it can attach a value to that key, and then
> while thread B is interrupted thread A looks at the dictionary and finds
> the key it's looking for, but with no valid reference as its value...
> [Disclaimer: I don't know the inner workings of dictionaries well enough
> to know if this exact situation is possible, but I do know that dicts
> are not threadsafe, so something *similar* is possible...])
My understanding was that the GIL is there to guarantee that python
statements are atomic. Now if your statements here are correct that
is not the case. So what is the GIL supposed to do?
More information about the Python-list