-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy patharturia_scheduler.py
50 lines (44 loc) · 1.97 KB
/
arturia_scheduler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import _heapq
import _random
import time
class Scheduler:
""" The purpose of this class is to provide a way for tasks to be scheduled in a thread-safe manner.
FL Studio's midi script does not allow usage of the threading library in Python. As such, we need to implement
a scheduler that runs on the OnIdle loop of the midi script. This allows one to schedule future tasks without
needing to worry about additional boiler-plate code. The only requirement is that this class's Refresh method
must be hooked up to midiscript's the OnIdle event.
"""
def __init__(self):
self._tasks_pq = []
self._random = _random.Random()
def ScheduleTask(self, task, delay=0):
time_ms = time.monotonic() * 1000
# Assumption is that there are no ties. Otherwise, comparing last element of the tuple will lead to crashes.
entry = (time_ms + delay,
len(self._tasks_pq),
# Add some random numbers so that one of these will break the tie
int(self._random.random() * 16777215),
int(self._random.random() * 16777215),
int(self._random.random() * 16777215),
int(self._random.random() * 16777215),
int(self._random.random() * 16777215),
task)
_heapq.heappush(self._tasks_pq, entry)
return entry
def CancelTask(self, entry):
try:
self._tasks_pq.remove(entry)
return True
except ValueError:
# Entry was already removed and executed.
return False
def Idle(self):
time_ms = time.monotonic() * 1000
while self._tasks_pq:
entry = _heapq.heappop(self._tasks_pq)
if entry[0] > time_ms:
# Entry delay condition not met. Put back on queue and wait until next refresh cycle.
_heapq.heappush(self._tasks_pq, entry)
return
task = entry[-1]
task()