Python : How to run a function "foo" per second and last a few minutes or hours

Scenario

I tried to run a function "foo" every second. I have to do this for a few minutes (say 5).

The function foo () sends 100 HTTP requests (including JSON objects) to the server and prints the JSON response.

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

Using threading.Timer

 #!/usr/bin/python import time import thread import threading  def foo():     print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())), threading.active_count()  #5 minutes = 5 * 60 = 300 seconds for x in range(0, 300):     t = threading.Timer(x + 1, foo)     t.start() 

Expected output

function foo was executed per second and last about 5 minutes

2020-05-14 00:54:49 301 2020-05-14 00:54:50 300 2020-05-14 00:54:51 299 2020-05-14 00:54:52 298 2020-05-14 00:54:53 297 2020-05-14 00:54:54 296 2020-05-14 00:54:55 295 ....... 2020-05-14 00:59:44 6 2020-05-14 00:59:45 5 2020-05-14 00:59:46 4 2020-05-14 00:59:47 3 2020-05-14 00:59:48 2 

Comments

  1. Free Bet No Deposit Bonus Offers: Best Sports Betting Offers
    The first thing you need to do is open an account with online sportsbooks and bet on the results of a sports 메이저 토토 사이트

    ReplyDelete

Post a Comment

Popular posts from this blog

How to fix error : no module named sendgrid when try to use sendgrid python lib in PHP.

react-native run-android : sun.security.provider.cert path.SunCertPathBuilderException : unable to find valid certification path to req uested target

react-native run-android : do not build/update modified code(App.js)