Home > Software design >  How to Execute Print Statement on Specific Timestamp in Python
How to Execute Print Statement on Specific Timestamp in Python

Time:02-01

I Just Want to Execute Print Statement in The Following Sequence

Here is My Code

Let's Assume The Following Time Stamp in Which Print Statement Should be Executed

t0 = 2022-01-25 12:00:00:000
t1 = 2022-01-25 12:00:00:050
t2 = 2022-01-25 12:00:00:100
t3 = 2022-01-25 12:00:00:150
t4 = 2022-01-25 12:00:00:200

With The Gap of 50 Milliseconds

Here is The Python Code

loop_start = 0
loop_end = 10

while loop_start < loop_end:
    print('Hello World')
    loop_start  = (1/20)

So I Want to Print on Every 50 Milliseconds But According to The System Time Don't Want To Use Delay Here.

It will Only Print According to The Timestamp Mentioned Above

Please Help Me To Resolve This

Thank You.!

CodePudding user response:

I've Solved This Through While Loop

loop_start = 0
loop_end = 10

start_time = (datetime.datetime.now(timezone.utc)).timestamp()

while loop_start < loop_end:
    start_time  = 0.05
    
    while (datetime.datetime.now(timezone.utc)).timestamp() < start_time:
            time.sleep(0.001)

    print('Hello World')
    loop_start  = (1/20)

So Here It will Check That Current Time is Equal To The Time Which is Required If Yes Then Print Otherwise Wait Till The Required Time

Still There is a Minor Delay In This But Acceptable For Me It Resolved My Problem

  •  Tags:  
  • Related