Starting work on librarian headless mode

This commit is contained in:
2024-04-24 12:00:56 +02:00
parent 7d29c1d397
commit 9d00e40b41
3 changed files with 50 additions and 4 deletions
@@ -1,3 +1,3 @@
habanero
waitress
flask
flask[async]
+4 -3
View File
@@ -68,7 +68,7 @@ def producer(out_q, control_q, filename, _logger):
# IMPORTANT!!! ONLY ONE CONSUMER THREAD AS WE ARE NOT PUTTING SENTINELS BACK
def consumer(in_q, control_q, doi, live_results, result_list, control_dict, _logger):
def consumer(in_q, control_q, doi, live_results, result_list, control_dict, no, _logger):
"""
Consumes items from an input queue and checks if DOI exists in the live results list.
@@ -79,6 +79,7 @@ def consumer(in_q, control_q, doi, live_results, result_list, control_dict, _log
live_results (list): List to store the search results.
_logger: Logger object for logging.
"""
_logger.info("Consumer thread started:%s", no)
for item in doi:
result_list.append({"DOI": item[0], "exists": False, "data": item[1]})
while True:
@@ -120,9 +121,9 @@ def search_for_doi(doi, live_results, _logger):
threads = []
work_q = Queue(maxsize=WORK_Q_SIZE)
control_q = Queue()
for _ in range (1, len(doi)//1000+1):
for i in range (0, (len(doi)//1000)+2):
t_cons = Thread(
target=consumer, args=(work_q, control_q, doi, live_results, result_list, control_dict, _logger)
target=consumer, args=(work_q, control_q, doi, live_results, result_list, control_dict, i, _logger)
)
_logger.info("Consumer thread created")
threads.append(t_cons)
+45
View File
@@ -0,0 +1,45 @@
import asyncio
import json
import logging
from json.decoder import JSONDecodeError
from logging import handlers
import requests
MAIN_BOT_ADDRESS = "http://192.168.1.191:5000"
SEND_RESULTS = "/conjurer"
async def send_results():
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
h1 = handlers.RotatingFileHandler(
filename="D:\\logs\\librarian.log",
encoding="utf-8",
mode="a",
maxBytes=6 * 1024 * 1024,
backupCount=6,
)
logger.addHandler(h1)
with open("s_results.json", "r+", encoding="utf-8") as s_file:
try:
database = json.load(s_file)
except JSONDecodeError:
pass
result = database["affd0dbc-e2c8-4173-b678-4103c0866495"]
result = {"affd0dbc-e2c8-4173-b678-4103c0866495":result}
coroutine = asyncio.to_thread(
requests.post,
f"{MAIN_BOT_ADDRESS}{SEND_RESULTS}",
json=result,
timeout=360,
)
logger.info("SENT")
result = await coroutine
logger.info(result.status_code)
logger.info("SEND CONFIRMED")
async def main():
await send_results()
if __name__ == "__main__":
asyncio.run(main())