-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdla_scheduler.py
39 lines (31 loc) · 1.29 KB
/
dla_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
import time
from multiprocessing import Process, Event, Queue
from config import Config, RefreshType
from dla_image import DLAImage
class DLAScheduler(Process):
def __init__(self, config: Config, queue: Queue, running_event: Event):
super().__init__(daemon=True)
self.config = config
self.running_event = running_event
self.queue = queue
self.dla_image = DLAImage(config)
if self.config.refresh == RefreshType.EVERY_TURN:
self._handle_turn = self._handle_turn_move
else:
self._handle_turn = self._handle_turn_growth
def run(self) -> None:
# Send init particles
self.queue.put(self.dla_image.grid)
# Run the simulation
while self.dla_image.grid_len < self.config.image_target_size:
self.running_event.wait()
self._handle_turn()
def _handle_turn_growth(self) -> None:
growth = self.dla_image.simulate_until_growth()
for particle in growth:
self.queue.put(particle)
def _handle_turn_move(self) -> None:
growth = self.dla_image.simulate_step()
self.queue.put([(p[0], p[1]) for p in self.dla_image.particles]) # Send travelling particles as a list
for particle in growth:
self.queue.put(particle)