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 7856499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:43:59+00:00 2026-06-02T20:43:59+00:00

I want to iterate over a JSON-API every 10th second, and do a second

  • 0

I want to iterate over a JSON-API every 10th second, and do a second HTTP-request using the same connection (keepalive) if a certain key was found in the JSON-data. If I do not place EM.stop in my code, the program stops to wait after finishing processing in req1.callback.

If I put EM.stop inside req2.callback it works and is iterating as expected.

But if the JSON-document did not include the key foobar, the program stops to wait after finishing processing in req1.callback.

If I add EM.stop in the last line inside req1.callback, req2.callback is aborted if the JSON-document had the key foobar.

How should I properly place EM.stop to make it iterate if the JSON-document had what I wanted or not?

require 'eventmachine'
require 'em-http'

loop do    
  EM.run do
    c = EM::HttpRequest.new 'http://api.example.com/'

    req1 = c.get :keepalive => true
    req1.callback do
      document = JSON.parse req1.response
      if document.has_key? foobar   
        req2 = c.get :path => '/data/'
        req2.callback do
          puts [:success, 2, req2]
          puts "\n\n\n"
          EM.stop
        end
      end
    end
  end

  sleep 10
end
  • 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-02T20:44:00+00:00Added an answer on June 2, 2026 at 8:44 pm

    If you want to use a timer, you should use the actual timer support from EM: http://eventmachine.rubyforge.org/EventMachine.html#M000467

    For example:

    require 'eventmachine'
    require 'em-http'
    
    EM.run do
      c = EM::HttpRequest.new 'http://google.com/'
      EM.add_periodic_timer(10) do
        # Your logic to be run every 10 seconds goes here!
      end
    end
    

    This way, you keep EventMachine running the whole time, instead of having to start/stop it every 10 seconds.

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

Sidebar

Related Questions

I want to iterate over an array of inputs that belong to certain class
I'm using Richfaces JSF and I want to iterate over an Map<Object,Object> . I
I want to iterate over two arrays at the same time, as the values
I have a JSON list that I want to iterate over, but skip the
I'm converting diverse JSON objects to structured objects and want to iterate over all
Specifically, I want to iterate over every element on the page, each time the
I am using T4 in Visual Studio 2010, and I want to iterate over
Is there a way to iterate over every property of an object using the
I have Map type. I want to iterate over it using struts tag library.
I want to iterate over files in a folder using powershell script;

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.