I am trying to use locks to write and read data in threads but have a deadlock I can't solve :s.
Here is the thing. For a reason that is not useful in my problem, I have to start a thread that will itself start other threads.
So I have :
I write in gpsData in stream_session, in an infinite loop (a new report is outputted every second).
I want to read those data in my main.
To avoid data corruption I want to use a blocking lock.
Here is the code :
lock = threading.Lock()
thread_gps = threading.Thread(None, send_gps_data, args=(gpsData, lock))
# to allow interrupt
gm.runn = 0
def send_gps_data(gpsData, lock):
gps_thread = threading.Thread(None, stream_session, args=(gpsData, lock))
def stream_session(gpsData, session, lock):
for report in session:
if report.get("class") == "TPV":
fill_gpsdata(gpsData, session, report)
try with calling the
lock.release method :
def stream_session(gpsData, session, lock): for report in session: if report.get("class") == "TPV": print ":" lock.acquire() fill_gpsdata(gpsData, session, report) lock.release() # better to call the method print "::"
And to answer to :
So it seems like my lock release is never taken into account. . .
yes, it will ignore the release of the lock, because you don't release it :) .