Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6928583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:12:27+00:00 2026-05-27T11:12:27+00:00

I am trying to use locks to write and read data in threads but

  • 0

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 :
main
-> send_gps_data
-> stream_session

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 :

def main():
    lock = threading.Lock()
    thread_gps = threading.Thread(None, send_gps_data, args=(gpsData, lock))  
    gps_thread_gps.start()

    # to allow interrupt
    while(gm.runn):
        try:
            time.sleep(0.5)
            print "!"
            lock.acquire():
            gm.display_gpsdata(gpsData)
            lock.release()
            print "!!"
        except KeyboardInterrupt:
            gm.runn = 0

def send_gps_data(gpsData, lock):
    global runn
    gps_thread = threading.Thread(None, stream_session, args=(gpsData, lock))    
    gps_thread.start()
    while(runn):
            time.sleep(1)
    gps_thread._Thread__stop()  
    sys.exit(0)

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
            print "::"

My problem is that I end up with a deadlock. Here is the output I get with the code :
!
!!
!
!!
:
::
!
:

And there it blocks. . .
So it seems like my lock release is never taken into account. . .

I tried to create lock as a global variable, but it doesn’t change anything.

Could you please help me on this? I really don’t see the problem

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:12:28+00:00Added an answer on May 27, 2026 at 11:12 am

    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 🙂 .

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use document.write(html); in my script but it looks illegal. I
LinkedList throws exception when trying to poll data. But I think i correctly use
I am trying to use Zend_Db_Select to write a select query that looks somewhat
I'm trying to use this library (which looks very nice) but I'm having difficulty
I am trying to use Windows Workflow and have a model that looks similar
Trying to use a guid as a resource id in a rest url but
I'm trying to use System.Runtime.InteropServices.ComTypes.IStream from C#, but I'm having some trouble. According to
I am trying to use the following code, but it does not work when
I'm learning to use the write function and am trying to print only a
I am trying to come up with the best data structure for use in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.