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

  • Home
  • SEARCH
  • 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 8757341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:16:42+00:00 2026-06-13T14:16:42+00:00

I have a database full of URLs that I need to test HTTP response

  • 0

I have a database full of URLs that I need to test HTTP response time for on a regular basis. I want to have many worker threads combing the database at all times for a URL that hasn’t been tested recently, and if it finds one, test it.

Of course, this could cause multiple threads to snag the same URL from the database. I don’t want this. So, I’m trying to use Mutexes to prevent this from happening. I realize there are other options at the database level (optimistic locking, pessimistic locking), but I’d at least prefer to figure out why this isn’t working.

Take a look at this test code I wrote:

threads = []
mutex = Mutex.new

50.times do |i|
  threads << Thread.new do
    while true do 
      url = nil

      mutex.synchronize do
        url = URL.first(:locked_for_testing => false, :times_tested.lt => 150)
        if url
          url.locked_for_testing = true
          url.save 
        end
      end

      if url
        # simulate testing the url
        sleep 1

        url.times_tested += 1
        url.save

        mutex.synchronize do
          url.locked_for_testing = false
          url.save
        end
      end
    end

    sleep 1
  end
end

threads.each { |t| t.join }

Of course there is no real URL testing here. But what should happen is at the end of the day, each URL should end up with “times_tested” equal to 150, right?

(I’m basically just trying to make sure the mutexes and worker-thread mentality are working)

But each time I run it, a few odd URLs here and there end up with times_tested equal to a much lower number, say, 37, and locked_for_testing frozen on “true”

Now as far as I can tell from my code, if any URL gets locked, it will have to unlock. So I don’t understand how some URLs are ending up “frozen” like that.

There are no exceptions and I’ve tried adding begin/ensure but it didn’t do anything.

Any ideas?

  • 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-06-13T14:16:44+00:00Added an answer on June 13, 2026 at 2:16 pm

    I’d use a Queue, and a master to pull what you want. if you have a single master you control what’s getting accessed. This isn’t perfect but it’s not going to blow up because of concurrency, remember if you aren’t locking the database a mutex doesn’t really help you is something else accesses the db.

    code completely untested

    require 'thread'
    queue = Queue.new
    keep_running = true
    
    # trap cntrl_c or something to reset keep_running
    master = Thread.new do 
      while keep_running
        # check if we need some work to do
        if queue.size == 0
          urls = URL.all(:times_tested.lt => 150)
          urls.each do |u|
            queue << u.id
          end
          # keep from spinning the queue
          sleep(0.1)
        end
      end
    end
    workers = []
    50.times do
      workers << Thread.new do
        while keep_running
          # get an id
          id = queue.shift
          url = URL.get(id)
          #do something with the url
          url.save
          sleep(0.1)
        end
      end
    end
    workers.each do |w|
      w.join
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database full of time-sensitive data, so on a daily basis I
I have a database with regular full-text catalogs. I want to detach this database,
I have a database full of text fields that look like this: (paragraph of
I have a database table full of time points and experimental values at those
We have an application that has a database full of polygons (currently stored as
I have an sqlite database full of huge number of URLs and it's taking
I have a database table full of game by game results and want to
I have a database full of autopart numbers that needs old part numbers prices
I have a database full of dates that are in the format: MM/dd/yy. For
I have a database full of php time(); stored when a user logs 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.