How is it possible to execute two functions with difference of every 5 seconds of time interval till the timer stops using threading in Python.
e.g:
def first_func():
print("First function")
def second_func():
print("Second function")
Result should be like this:
-------------------------------
First Function At starts
Second Function 5 Seconds
First Function 10 Seconds
Second Function 15 Seconds
First Function 20 Seconds
Second Function 25 Seconds
First Function 30 Seconds
And so on.
Try to use multiprocessing
def you_function1(*args, **kwargs):
pass
def you_function2(*args, **kwargs):
pass
from multiprocessing import Process
first_process = Process(target=you_function1)
second_process = Process(target=you_function2)
first_process.start()
second_process.start()
first_process.join()
second_process.join()
https://docs.python.org/2/library/multiprocessing.html
Threading is pretty straightforward for this:
import threading
import time
def first_func():
while True:
print("First function",time.time()-start)
time.sleep(10)
def second_func():
time.sleep(5)
while True:
print("Second function",time.time()-start)
time.sleep(10)
start = time.time()
t1 = threading.Thread(target=first_func)
t1.start()
t2 = threading.Thread(target=second_func)
t2.start()
Output:
First function 0.0
Second function 5.008664846420288
First function 10.002728939056396
Second function 15.010392904281616
First function 20.018056869506836
Second function 25.02572202682495
Related
I have a main function, where all the tasks are located. The first task I want to run continuous. My second task I want to run on a specific date (For example: Every Monday at 12AM). The last task must rerun the the main function but with other parameters (I want to run this task every hour).
I'm using Python on Ubuntu 18.
I've tried to use the module 'schedule' and searched for an answer here on stack overflow, google, ... but I didn't found anything useful.
import schedule, time
def main(par1, par2, par3):
def task1():
# Do something
print("Executing task1")
def task2():
# Do something different
print("Executing task2")
def rerunTask():
print("Reruning main task")
main(1,2,3) # Rerun main function with other parameters
schedule.every().monday.at("12:00").do(task2)
schedule.every(0.5).seconds.do(task1)
schedule.every().hour.do(rerunTask)
main(2,3,1)
When I tried this code everything worked fine until the "rerun task". After he executes this task he continuous reruns this function for the rest of the time.
Can someone please help me?
You could use time library with threading library and based on epochs value, the function will be executed.
Warning: Because of the use of thread, you might have to kill the terminal to exit.
import time, threading
def main(par1, par2, par3):
def task1():
# Do something
print("Executing task1")
def task2():
# Do something different
print("Executing task2")
def run_task1():
while(1):
task1()
time.sleep(0.5)
def run_task2():
while(1):
task2()
time.sleep(3600)
def run_task3():
week_diff = 604800
curr_time = time.time()
first_monday_epoch = 345600
total_monday_crossed = int((curr_time - first_monday_epoch) / week_diff)
next_monday = (total_monday_crossed + 1) * week_diff + first_monday_epoch
time.sleep(next_monday - time.time())
while(1):
task2()
time.sleep(604800) #week time difference
t1 = threading.Thread(target=run_task1, args=())
t2 = threading.Thread(target=run_task2, args=())
t3 = threading.Thread(target=run_task3, args=())
t1.start()
t2.start()
t3.start()
t1.join()
t2.join()
t3.join()
main(2,3,1)
Note: I used Epoch converter to calculate epoch of first monday 00:00 AM and other epoch information.
I have a simple python code. It makes something and it sleeps x second and it makes another thing again. I want to extend that sleeping time after I execute the file.
For example
print("A")
time.sleep(x)
print("B")
After I execute the code I want to change and increase the sleeping time before it prints B.
Is it possible? Please help. Thanks.
This solution is using a list with sleep times you can expand by appending further sleeps while your target function is sleeping. The function sleep_all just pops off and executes any sleep available from that list.
import time
from datetime import datetime
from threading import Thread, current_thread
def f():
print(f'{datetime.now()}: A')
sleep_all()
print(f'{datetime.now()}: B')
def sleep_all():
sleeps = current_thread().sleeps
while sleeps:
sleep = sleeps.pop()
print(f'{datetime.now()}: taking a nap for {sleep} s')
time.sleep(sleep)
if __name__ == '__main__':
t = Thread(target=f)
t.sleeps = [5] # attach instance attribute with sleeps-list
t.start()
time.sleep(2)
print(f'{datetime.now()}: adding another sleep')
t.sleeps.append(5)
Example Output:
2018-12-07 22:54:09.733494: A
2018-12-07 22:54:09.733553: taking a nap for 5 s
2018-12-07 22:54:11.735635: adding another sleep
2018-12-07 22:54:14.734963: taking a nap for 5 s
2018-12-07 22:54:19.738833: B
Process finished with exit code 0
Version with subclassing Thread:
import time
from datetime import datetime
from threading import Thread
class Program(Thread):
def __init__(self, sleep=None):
super().__init__()
self._sleeps = [sleep]
def run(self):
print(f'{datetime.now()}: A')
self._sleep_all()
print(f'{datetime.now()}: B')
def add_sleep(self, sleep):
self._sleeps.append(sleep)
def _sleep_all(self):
while self._sleeps:
sleep = self._sleeps.pop()
print(f'{datetime.now()}: taking a nap for {sleep} s')
time.sleep(sleep)
if __name__ == '__main__':
prg = Program(sleep=5)
prg.start()
time.sleep(2)
print(f'{datetime.now()}: adding another sleep')
prg.add_sleep(sleep=5)
prg.join()
#!/usr/bin/env python
import time
try:
with open(".sleeptime") as f:
sleep_time = float(f.read())
except FileNotFoundError:
sleep_time = 5.0
with open(".sleeptime", "w") as f:
next_sleep_time = sleep_time + 5.0
f.write(str(next_sleep_time))
print("A")
time.sleep(sleep_time)
print("B")
If I have a function called a lot of times in a for loop and this function sometimes is running too much time, how can I use a timer for each call of function(to set and reset the timer each time)?
It looks like:
def theFunction(*args):
#some code (timer is on)
#In this point time is out, break and exit function
#Timer is reseted
for i in range(0,100):
theFunction(*args)
Use the time module like so:
import time
time_start = time.time()
#Do function stuff
time_stop = time.time()
#Check your time now
timed_segment = time_stop - time_start
#Repeat if needed
To run this multiple times in a for loop you will need to append times into a list as it runs like so:
import time
def function():
times_list = []
for x in range(10)
time_start = time.time()
#Do function stuff
time_stop = time.time()
#Check your time now
timed_segment = time_stop - time_start
times_list.append(timed_segment)
#Repeat as many times as needed
return times_list
If you want to break after a certain amount of time you can use a while loop instead like so:
import time
def function():
times_list = []
time_start = time.time()
time_end = time.time()
while time_end - time_start < 10: #after 10 seconds the while loop will time out
#Your function does stuff here
time_end = time.time()
#Next, append times to a list if needed
time_list.append(time_start - time_end)
return times_list
To stop the function after a certain time regardless of where it is, we can use threading like so:
import threading
from time import sleep
def do_stuff():
sleep(10)
print("1 + 2")
return
t = threading.Thread(target=do_stuff)
t.start()
t.join(timeout = 5)
In the above example, calling timeout in join will kill the thread after 5 seconds. We can also put this into a decorator if we plan on reusing it many times like so:
import threading
from time import sleep
def timeout(func):
def inner_func(*nums, **kwargs):
t = threading.Thread(target=func, args=(*nums,))
t.start()
t.join(timeout=5)
return inner_func
#timeout
def do_stuff(a,b):
sleep(3)
print(a+b)
return
do_stuff(1,3)
There is another module called timeit which can measure the execution time of small code snippets. I believe you can use that also. I have never used that module but it should work.
Here is the link to the doc page. Give it a look :: https://docs.python.org/2/library/timeit.html
see How to use timeit module as well
For high re-usability and ease of implementations, I would recommend -
Using decorators -
from time import time
def time_it(func):
def wrapper(*args, **kwargs):
a=time()
func(*args, **kwargs)
print(a-time())
return wrapper
#time_it
def foo(s='this works'):
print(s)
foo()
Using profile.run - https://docs.python.org/2/library/profile.html#module-profile
I want to make my function run for a particular period like for 5 seconds; how I can do that ?
Like,
def my_function():
while(time == 10 seconds):
......... #run this for 10 seconds
def my_next_function():
while(time == 5 seconds):
......... #run this for 5 seconds
This will definitely help you.
import time
def myfunc():
now=time.time()
timer = 0
while timer != 10:
end = time.time()
timer = round(end-now)
def mynextfunc():
now=time.time()
timer = 0
while timer != 5:
end = time.time()
timer = round(end-now)
myfunc()
print "myfunc() exited after 10 seconds"
mynextfunc()
print "mynextfunc() exited after 5 seconds"
I'd use a <=, rather than a != there. With the round, you'll get integer times, but if something ugly happens, and you skip a second, it'll run forever!
If an individual loop iteration does not take much time:
#!/usr/bin/env python3
from time import monotonic as timer
def my_function():
deadline = timer() + 10
while timer() < deadline:
......... #run this for 10 seconds
def my_next_function():
deadline = timer() + 5
while timer() < deadline:
......... #run this for 5 seconds
Otherwise, see How to limit execution time of a function call in Python.
I'm assuming you want to repeat the whole function until time is up, rather than trying to interrupt the function midway through it time is up (which would be more difficult). One nice solution is to use a decorator:
import time
def repeat(func):
def inner(*args, **kwargs):
if 'repeat_time' in kwargs:
stop = kwargs.pop('repeat_time') + time.time()
while time.time() <= stop:
func(*args, **kwargs)
else:
func(*args, **kwargs)
return inner
#repeat
def my_func():
# ...
my_func() # calls my_func once
my_func(repeat_time=10) # repeatedly calls my_func for 10 seconds
This code assumes you don't want to do anything with the return values from my_func, but can easily be adapted to collect the return values in case you do.
Or simpler if you do not need to pass any parameters to my_func:
def repeat_for(seconds, func):
stop = seconds + time.time()
while time.time() <= stop:
func()
def my_func():
# ...
repeat_for(10, my_func)
I want to write a code which execute a statement specified number of times per second,
Many of you might be familier about the term rate
Here i want rate to be 30 per second
say i want to execute a function 30 times per second for 60 seconds
means rate=30/sec duration=60sec
Can any one tell me is their any api available in python to do the same ?
The sched module is intended for exactly this:
from __future__ import division
import sched
import time
scheduler = sched.scheduler(time.time, time.sleep)
def schedule_it(frequency, duration, callable, *args):
no_of_events = int( duration / frequency )
priority = 1 # not used, lets you assign execution order to events scheduled for the same time
for i in xrange( no_of_events ):
delay = i * frequency
scheduler.enter( delay, priority, callable, args)
def printer(x):
print x
# execute printer 30 times a second for 60 seconds
schedule_it(1/30, 60, printer, 'hello')
scheduler.run()
For a threaded environment, the use of sched.scheduler can be replaced by threading.Timer:
from __future__ import division
import time
import threading
def schedule_it(frequency, duration, callable, *args, **kwargs):
no_of_events = int( duration / frequency )
for i in xrange( no_of_events ):
delay = i * frequency
threading.Timer(delay, callable, args=args, kwargs=kwargs).start()
def printer(x):
print x
schedule_it(5, 10, printer, 'hello')
Try using threading.Timer:
def hello():
print "hello, world"
t = Timer(30.0, hello)
t.start() # after 30 seconds, "hello, world" will be printed
You can use time.time() to do what you want:
import time
def your_function():
# do something...
while True:
start = time.time() # gives current time in seconds since Jan 1, 1970 (in Unix)
your_function()
while True:
current_time = time.time()
if current_time - start >= 1.0/30.0:
break
This will make sure that the delay between calls of your_function is very close to 1/30 of a second, even if your_function takes some time to run.
There is another way: using Pythons built-in scheduling module, sched. I never used it, so I can't help you there, but have a look at it.
After some time spending i discovered how to do it well i used multiprocessing in python to achieve it
here's my solution
#!/usr/bin/env python
from multiprocessing import Process
import os
import time
import datetime
def sleeper(name, seconds):
time.sleep(seconds)
print "PNAME:- %s"%name
if __name__ == '__main__':
pros={}
processes=[]
i=0
time2=0
time1=datetime.datetime.now()
for sec in range(5):
flag=0
while flag!=1:
time2=datetime.datetime.now()
if (time2-time1).seconds==1:
time1=time2
flag=1
print "Executing Per second"
for no in range(5):
i+=1
pros[i] = Process(target=sleeper, args=("Thread-%d"%i, 1))
j=i-5
for no in range(5):
j+=1
pros[j].start()
j=i-5
for no in range(5):
j+=1
processes.append(pros[j])
for p in processes:
p.join()