Python Time Delays

Python Time Delays

Have a look at threading.Timer. It runs your function in a new thread.

from threading import Timer

def hello():
print "hello, world"

t = Timer(30.0, hello)
t.start() # after 30 seconds, "hello, world" will be printed

How to add time delay for every 10 lists of completion in python threadpool execution?

The straightforward way is to just submit up to 10 jobs at a time, then sleep between each chunk:

import itertools
import time
from concurrent.futures import ThreadPoolExecutor

# See https://stackoverflow.com/a/8991553/51685
def chunker(n, iterable):
it = iter(iterable)
while True:
chunk = tuple(itertools.islice(it, n))
if not chunk:
return
yield chunk

def parse(user):
return f"{user} parsed!"

def main():
user_list = list(range(100))
with ThreadPoolExecutor(max_workers=10) as exe:
for chunk in chunker(10, user_list):
start = time.time()
result = exe.map(parse, chunk)
output = list(result)
end = time.time()
print(output, "taken time", end - start)
time.sleep(1)

if __name__ == "__main__":
main()

This prints out e.g.

['0 parsed!', '1 parsed!', '2 parsed!', '3 parsed!', '4 parsed!', '5 parsed!', '6 parsed!', '7 parsed!', '8 parsed!', '9 parsed!'] taken time 0.0006809234619140625
['10 parsed!', '11 parsed!', '12 parsed!', '13 parsed!', '14 parsed!', '15 parsed!', '16 parsed!', '17 parsed!', '18 parsed!', '19 parsed!'] taken time 0.0008037090301513672
['20 parsed!', '21 parsed!', '22 parsed!', '23 parsed!', '24 parsed!', '25 parsed!', '26 parsed!', '27 parsed!', '28 parsed!', '29 parsed!'] taken time 0.0008540153503417969
...

EDIT for tqdm progress

To use tqdm with this approach so it gets updated on each parse step, you'll need something like the below (bits identical to the above replaced with ...).

(tqdm won't update the screen unless enough time has passed since the last time it did, hence the random sleep to represent work done.)

def parse(user, prog):
time.sleep(random.uniform(.1, 1.3)) # Do work here...
prog.update() # Step the progress bar.
return f"{user} parsed!"

def main():
# ...
with ThreadPoolExecutor(max_workers=10) as exe, tqdm.tqdm(total=len(user_list)) as prog:
for chunk in chunker(10, user_list):
# ...
result = exe.map(parse, chunk, [prog] * len(chunk))
# ...

How can I specify Time Delays in Linear Systems in Python?

As far as I know there are no widely supported control libraries for Python which support delays in the same way as the Matlab control toolbox does. My students and I have been working on a solution for this for a while and hope to package it up for release to pypi this year.

The repository with our code is here and the object which allows the kinds of manipulations you're talking about using an internal delay representation is defined in InternalDelay.py.

One slight problem with your example is that your Q is not physically realisable, which causes some errors in our library.

The following code will produce a step response for your closed loop system, showing the effect of adding a delay. I've also used only PI control instead of PID to keep the controller physically realisable.

from utils import InternalDelay, tf
import matplotlib.pyplot as plt
import numpy as np

s = tf([1, 0], 1)
G = 1/(s + 1)
Q = 1 + 2/s
H = tf(1, 1, deadtime=0.1)
G = InternalDelay(G)
Q = InternalDelay(Q)
H = InternalDelay(H)
one = InternalDelay(tf(1, 1))

M = Q*G/(one + Q*G)
Mdelay = Q*G/(one + Q*G*H)

t = np.linspace(0, 10, 5000)

y = M.simulate(lambda t: [1], t)
ydelay = Mdelay.simulate(lambda t: [1], t)

plt.plot(t, y, t, ydelay)
plt.legend(['Delay-free', 'Delay=0.1s'])
plt.axhline(1)

example

Time delay in python

This should work.

import time
print "Think of a number between 1 and 100"
print "Then I shall guess the number"

time.sleep(3)

print "I guess", computerguess
raw_input ("Is it lower or higher?")


Related Topics



Leave a reply



Submit