Skip to content

Commit

Permalink
feat: use threading or forking for DAQJobServeHTTP
Browse files Browse the repository at this point in the history
- it uses threading for windows and forking for others, as windows does not support forking processes
  • Loading branch information
furkan-bilgin committed Nov 2, 2024
1 parent 66c349f commit b61c039
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/daq/jobs/serve_http.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import http.server
import socketserver
import threading
from dataclasses import dataclass
from datetime import datetime, timedelta

from daq.base import DAQJob
from daq.models import DAQJobConfig

try:
from socketserver import ForkingMixIn # type: ignore
except ImportError:
# For Windows
from socketserver import ThreadingMixIn as ForkingMixIn


@dataclass
class DAQJobServeHTTPConfig(DAQJobConfig):
Expand All @@ -26,6 +31,9 @@ def start(self):
# Start a BasicHTTPServer in this thread
serve_path = self.config.serve_path

class ThreadingHTTPServer(ForkingMixIn, http.server.HTTPServer): # type: ignore
pass

class Handler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=serve_path, **kwargs)
Expand All @@ -46,7 +54,7 @@ def log_message(self, format: str, *args) -> None:
pass

def start_server():
with socketserver.TCPServer(
with ThreadingHTTPServer(
(self.config.host, self.config.port), Handler
) as httpd:
self._logger.info(
Expand Down

0 comments on commit b61c039

Please sign in to comment.