Jackem
Jackem

Reputation: 31

two python script in loop the other one have timer while other don't have

is it possible to run two script in python that the other one have a timer every sec in while loop while the other one don't have a timer in while loop

I already try to put it in the same while loop but the other one has been affected by the one that have a timer any idea guys

and also I forgot to say they will run infinitely

thanks in advance

Upvotes: 2

Views: 82

Answers (2)

Natmo
Natmo

Reputation: 26

You could use multiprocessing to process two Infinite While loop:

from multiprocessing import process

def one():
 while True:
  print("hello world")

def two():
 while True:
  Print("lovely")
  time.sleep(2)

if __name__=='__main__':
 f=Process(target=one)
 f.start()

 k=Process(target=two)
 k.start()

Upvotes: 1

BladeOfLightX
BladeOfLightX

Reputation: 534

Here is a simple python program where two loops run parallel.

First function prints 1 to 50 in first 10 second and then other function gets activated and runs after 10 second

import threading
import time

print("The other process will run after 10 sec")
def threadFunc():
    time.sleep(10)
    
    j = 0
    
    while j < 50:
        print("J is at {}".format(j))
        
        j += 1
        
        time.sleep(1.5) 
        
thread = threading.Thread(target=threadFunc)

thread.start()

i = 0

while i < 50:
    print('I variable is now at {}'.format(i))
    
    i += 1
    
    time.sleep(1)
    
    

Upvotes: 0

Related Questions