jztools.pacers.pacers#
Classes
|
Ensures a maximum number of calls are running simultaneously. |
|
Ensures a minimum period before the consecutive entries into the context manager in any thread sharing the pacer. |
- class jztools.pacers.pacers.RatePacer(rate)#
Bases:
objectEnsures a minimum period before the consecutive entries into the context manager in any thread sharing the pacer. The actual period will be slightly lower. True periods are guaranteed to be higher than the nominal period, with precision around 5% of nominal period for periods=10ms. Higher period result in better precision.
import time, threading rate = 100 # 10 ms times = [] def run(th_id, pacer, iters=10): for k in range(iters): with pacer: times.append((th_id, time.time())) # pacer = RatePacer(rate) threads = [threading.Thread(target=run, args=(th_id, pacer)) for th_id in range(10)] [_t.start() for _t in threads] [_t.join() for _t in threads] # times.sort(key=lambda x: x[1]) periods = np.diff(np.array(times)[:, 1])
- Parameters:
period – Minimum time between last exit and next enter.
timeout – Timeout after this many seconds when waiting to enter the pacer.
- class jztools.pacers.pacers.ConcurrencyPacer(num: int, timeout=-1)#
Bases:
objectEnsures a maximum number of calls are running simultaneously.
- Parameters:
num – Max number of simultaneous calls.
timeout – Timeout after this many seconds when waiting to enter the pacer.