Skip to content

Commit

Permalink
refactor: return union of daq job and thread when starting daq jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
furkan-bilgin committed Oct 8, 2024
1 parent 97d6d5e commit 524419b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/daq/daq_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import tomllib

from daq.caen.n1081b import DAQJobN1081B
from daq.models import DAQJob, DAQJobConfig
from daq.models import DAQJob, DAQJobConfig, DAQJobThread

DAQ_JOB_TYPE_TO_CLASS: dict[str, type[DAQJob]] = {
"n1081b": DAQJobN1081B,
Expand Down Expand Up @@ -40,14 +40,14 @@ def load_daq_jobs(job_config_dir: str) -> list[DAQJob]:
return jobs


def start_daq_job(daq_job: DAQJob) -> threading.Thread:
def start_daq_job(daq_job: DAQJob) -> DAQJobThread:
thread = threading.Thread(target=daq_job.start, daemon=True)
thread.start()

return thread
return DAQJobThread(daq_job, thread)


def start_daq_jobs(daq_jobs: list[DAQJob]) -> list[threading.Thread]:
def start_daq_jobs(daq_jobs: list[DAQJob]) -> list[DAQJobThread]:
threads = []
for daq_job in daq_jobs:
threads.append(start_daq_job(daq_job))
Expand Down
7 changes: 7 additions & 0 deletions src/daq/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import threading
from dataclasses import dataclass
from typing import Any

Expand Down Expand Up @@ -26,3 +27,9 @@ def start(self):

def stop(self):
self._should_stop = True


@dataclass
class DAQJobThread:
daq_job: DAQJob
thread: threading.Thread
4 changes: 2 additions & 2 deletions src/test_entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
logging.basicConfig(level=logging.DEBUG)

daq_jobs = load_daq_jobs("configs/")
threads = start_daq_jobs(daq_jobs)
daq_job_threads = start_daq_jobs(daq_jobs)

while True:
any_thread_alive = any(t.is_alive() for t in threads)
any_thread_alive = any(t.thread.is_alive() for t in daq_job_threads)
if not any_thread_alive:
break

Expand Down

0 comments on commit 524419b

Please sign in to comment.