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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:30:21+00:00 2026-06-10T02:30:21+00:00

I want to simulate time in Python. I want to apply my_fct every second,

  • 0

I want to simulate time in Python. I want to apply “my_fct” every second, however I do not know how long “my_fct” takes to run so I cannot use a sleep.
What I did is this:

    past_time =  datetime.datetime.utcnow()
    present_time =  datetime.datetime.utcnow()
    for i in range(10):
        while( (present_time - past_time).total_seconds()  < 1):
            present_time = datetime.datetime.utcnow()
        my_fct(......)
        past_time = present_time

I don’t think it is the good way to do it? What is the right solution?
Thank you

  • 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-10T02:30:22+00:00Added an answer on June 10, 2026 at 2:30 am

    running your code gives:

    >>> 
    
    Traceback (most recent call last):
      File "C:\python\tester.py", line 6, in <module>
        while((present_time - past_time) < 1):
    TypeError: can't compare datetime.timedelta to int
    

    what you need is this:

    import datetime
    
    past_time =  datetime.datetime.utcnow()
    present_time =  datetime.datetime.utcnow()
    for i in range(10):
        while((present_time - past_time).seconds < 1):
            present_time = datetime.datetime.utcnow()
        my_fct(......)
        past_time = present_time
    

    time-time gives a timedelta value. u need the seconds of this value. and then compare to your int.

    edit: however, i now realize this is not the solution you are looking for.

    this is a way to do it with threads (a very simple way – prone to problems depending on what my_fct() does.)

    import time
    from threading import Thread
    
     for i in range(10):
        time.sleep(1)
        t = Thread(target=my_fnc, args=(......))
        t.start()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to simulate a click, $(#genStats).trigger(click); every time I refresh the page window.location.reload()
I want to simulate many key press events. I found a solution by using
I want to simulate stroking a carpet, so you would have a graphic of
I would want to simulate the behavior of a table with div. I have
Lets say I want to simulate a particle state, which can be normal (0)
I have an application where I want to simulate the connection between a device
I have a 32GB memory machine and I want to simulate trying out one
I'm developing a Silverlight application where I want to simulate a console. There are
I have multiple buttons on a page and I want to simulate a click
I'm developing a firefox extension and i want to simulate :hover state of an

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.