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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:45:24+00:00 2026-06-12T03:45:24+00:00

I am trying to run a certain function foo every second. I have to

  • 0

I am trying to run a certain function “foo” every second. I have to do this for a few minutes (say 5).

The function foo() makes 100 HTTP Requests (which contains a JSON object) to the server and prints the JSON response.

In short, I have to make 100 HTTP requests per second for 5 minutes.

I have just started learning python, thus don’t have extensive knowledge. This is what I have tried:

import threading
noOfSecondsPassed = 0
def foo():
   global noOfSecondsPassed
   # piece of code which makes 100 HTTP requests (I use while loop)
   noOfSecondsPassed += 1

while True:
   if noOfSecondsPassed < (300)  # 5 minutes
       t = threading.Timer(1.0, foo)
       t.start()

Due to multiple threads, the function foo isn’t called 300 times but much much more than that.
I have tried setting a lock too:

def foo():
  l = threading.Lock()
  l.acquire()
  global noOfSecondsPassed
  # piece of code which makes 100 HTTP requests (I use while loop)
  noOfSecondsPassed += 1
  l.release()

Rest of code is same as the previous code snippet. But this also does not work.

How do I do this?

Edit: Different Approach

I have tried this approach which worked for me:

def foo():
    noOfSecondsPassed = 0
    while noOfSecondsPassed < 300:
       #Code to make 100 HTTP requests
       noOfSecondsPassed +=1
       time.sleep(1.0)
foo()

Any disadvantages in doing so?

  • 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-12T03:45:25+00:00Added an answer on June 12, 2026 at 3:45 am

    I would use another approach which is easier I think.

    Create 300 timer thread, each running 1 sec after the previous. The main loop is executed in almost an instant so the error factor is very low.
    Here’s a sample Demo:

    import datetime
    import thread
    import threading
    
    def foo():
         print datetime.datetime.now()
         print threading.active_count()
    
    for x in range(0,300): 
         t = threading.Timer(x + 1, foo)
         t.start()
    

    This code output should look like this:

    2012-10-01 13:21:07.328029
    301
    2012-10-01 13:21:08.328281
    300
    2012-10-01 13:21:09.328449
    299
    2012-10-01 13:21:10.328615
    298
    2012-10-01 13:21:11.328768
    297
    2012-10-01 13:21:12.329006
    296
    2012-10-01 13:21:13.329289
    295
    2012-10-01 13:21:14.329369
    294
    2012-10-01 13:21:15.329580
    293
    2012-10-01 13:21:16.329793
    292
    2012-10-01 13:21:17.329958
    291
    2012-10-01 13:21:18.330138
    290
    2012-10-01 13:21:19.330300                                                                                                                                                                                                                         
    289                         
    ...
    

    As you can see, each thread is launched about 1 sec after the previous and you are starting exactly 300 threads.

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

Sidebar

Related Questions

I'm currently trying to run a certain procedure (sql 2005) in VB6, passing some
I'm trying to run JBoss with sudo under certain configuration. I type: sudo jboss
Trying to run my program in FreeBSD OS, I have the following results: $
Im trying to run exe file with c++ with no luck I tried this:
I am trying to automatically jump to the second line after a certain number
I'm trying to output the following each time a function is run: <object id=video
I have a problem when trying to insert certain characters through query with parameters.
I am trying to run some script when div reaches to a certain point
I'm working on a Titanium app and have build a function to run sqlite
I have a Windows Service which performs a certain function, and then needs to

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.