-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
461 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import logging | ||
import threading | ||
from datetime import datetime | ||
from enum import Enum | ||
from wannadb.data.data import Attribute, Document | ||
from wannadb.statistics import Statistics | ||
from wannadb_web.worker.Web_API import Web_API | ||
from wannadb.resources import ResourceManager | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Status(Enum): | ||
"""Gives the status of the application.""" | ||
IDLE = 1 | ||
RUNNING = 2 | ||
CREATED = 3 | ||
DEAD = 98 | ||
ERROR = 99 | ||
|
||
|
||
class Web_API_Thread(threading.Thread): | ||
def __init__(self, thread_id): | ||
super().__init__() | ||
self.function = None | ||
self.thread_id = thread_id | ||
self.wannadb_web_api = Web_API() | ||
self.event = threading.Event() | ||
self.status = Status.IDLE | ||
self.last_call = datetime.now() | ||
self.exit_flag = False | ||
|
||
def run(self): | ||
ResourceManager() | ||
self.status = Status.RUNNING | ||
while True: | ||
if self.exit_flag: | ||
self.status = Status.DEAD | ||
logger.info(f"Thread {self.thread_id} exited") | ||
return | ||
self.event.wait() | ||
self.event.clear() | ||
if self.function is not None: | ||
self.function() | ||
self.last_call = datetime.now() | ||
else: | ||
raise Exception("No function set") | ||
self.function = None | ||
|
||
def create_document_base(self, documents: [Document], attributes: [Attribute], statistics: Statistics): | ||
if self.function is not None: | ||
raise Exception("Function running") | ||
self.function = lambda: self.wannadb_web_api.create_document_base_task(documents, attributes, statistics) | ||
self.event.set() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import threading | ||
import logging.config | ||
import time | ||
from datetime import datetime | ||
from wannadb_web.worker.Web_API_Thread import Web_API_Thread | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Web_Thread_Manager(threading.Thread): | ||
def __init__(self, idle_time=60): | ||
super().__init__() | ||
logger.info("Web_Thread_Manager initialized") | ||
self.idle_time = idle_time | ||
self.threads: dict[int, Web_API_Thread] = {} | ||
self.thread_limit = 2 | ||
global web_Thread_Manager | ||
web_Thread_Manager = self | ||
|
||
def run(self): | ||
logger.info("Web_Thread_Manager running") | ||
while True: | ||
time.sleep(self.idle_time) | ||
for thread_id, thread in self.threads.items(): | ||
if not thread.is_alive(): | ||
logger.info(f"Thread {thread_id} cleaned") | ||
del self.threads[thread_id] | ||
elif (datetime.now() - thread.last_call).total_seconds() > self.idle_time: | ||
thread.exit_flag = True | ||
|
||
def access_thread(self, thread_id): | ||
if thread_id not in self.threads: | ||
logger.error("Thread not found") | ||
raise threading.ThreadError("Thread not found") | ||
logger.debug(f"Thread {thread_id} accessed") | ||
return self.threads[thread_id] | ||
|
||
def new_thread(self, thread_id): | ||
if thread_id in self.threads: | ||
logger.debug(f"Thread {thread_id} already exists") | ||
return self.threads[thread_id] | ||
if len(self.threads) >= self.thread_limit: | ||
logger.error("Thread limit reached") | ||
raise threading.ThreadError("Thread limit reached") | ||
thread = Web_API_Thread(thread_id) | ||
thread.start() | ||
logger.debug(f"Thread {thread_id} created and started") | ||
return thread |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.