add main scripts for converter

This commit is contained in:
Jeniamakarchik
2020-01-16 18:17:39 +03:00
parent a9cea63542
commit 653b4c934b
2 changed files with 677 additions and 0 deletions

81
src/consumer.py Normal file
View File

@@ -0,0 +1,81 @@
import json
import os
from threading import Thread, active_count
import pika
from book import Book
# from src.book import Book
class Consumer:
def __init__(self, url):
self._connection = None
self._channel = None
self._closing = False
self._url = url
def run(self):
pass
def close_connection(self):
pass
def convert_book(file_path, output=None, recreate=True, train_mode=False, convert=False, model_location=None):
logging_format = '%(asctime)s - %(levelname)s - %(message)s'
folder_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
file_path = os.path.join(folder_path, file_path)
book = Book(file_path, output, recreate, train_mode, convert, model_location)
book.conversion(logging_format=logging_format)
print('Book has been proceeded.')
range(10)
def callback(ch, method, properties, body):
print(f'Message: {body}.')
try:
data = json.loads(body)
thread = Thread(target=convert_book, kwargs=data)
thread.start()
print(f'Active threads: {active_count()}.')
except Exception as e:
print(e)
pass
finally:
# thread.join()
print('Waiting for the message...')
if __name__ == '__main__':
folder_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
config_path = os.path.join(folder_path, "test_config/config.json")
with open(config_path, "r") as f:
conf_param = json.load(f)
# credentials = pika.PlainCredentials('admin', 'admin')
# parameters = pika.ConnectionParameters('10.40.10.173', credentials=credentials)
credentials = pika.PlainCredentials(username=conf_param['username'], password=conf_param['password'])
parameters = pika.ConnectionParameters(host=conf_param['host'], credentials=credentials)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()
try:
channel.queue_declare(queue=conf_param['queue'], passive=True)
except Exception as e:
print(e)
raise
channel.basic_consume(queue=conf_param['queue'],
auto_ack=True,
on_message_callback=callback)
print('Waiting for messages...')
channel.start_consuming()