Update consumer.py

update reading configurations
This commit is contained in:
Jeniamakarchik
2020-02-07 12:03:07 +03:00
parent 258f3518bf
commit af6230d072

View File

@@ -1,6 +1,7 @@
import json import json
import os import os
from functools import partial from functools import partial
from pathlib import Path
from threading import Thread, active_count from threading import Thread, active_count
import pika import pika
@@ -26,7 +27,7 @@ def callback(ch, method, properties, body, access):
'book_id': data['id'], 'book_id': data['id'],
'access': access 'access': access
} }
thread = Thread(target=convert_book, kwargs=params) thread = Thread(target=convert_book, kwargs=params)
thread.start() thread.start()
print(f'Active threads: {active_count()}.') print(f'Active threads: {active_count()}.')
@@ -42,13 +43,16 @@ def callback(ch, method, properties, body, access):
if __name__ == '__main__': if __name__ == '__main__':
folder_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) folder_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
config_path = os.path.join(folder_path, "config/config.json") # config_path = Path(os.path.join(folder_path, "config/config.json"))
# config_path = os.path.join(folder_path, "config/queue_config.json") config_path = Path(os.path.join(folder_path, "config/queue_config.json"))
with open(config_path, "r") as f: with open(config_path, "r") as f:
conf_param = json.load(f) conf_param = json.load(f)
host = conf_param.get('host') or pika.ConnectionParameters().DEFAULT_HOST
port = conf_param.get('port') or pika.ConnectionParameters().DEFAULT_PORT
credentials = pika.PlainCredentials(username=conf_param['username'], password=conf_param['password']) credentials = pika.PlainCredentials(username=conf_param['username'], password=conf_param['password'])
parameters = pika.ConnectionParameters(host=conf_param['host'], credentials=credentials) parameters = pika.ConnectionParameters(host=host, port=port, credentials=credentials)
connection = pika.BlockingConnection(parameters) connection = pika.BlockingConnection(parameters)
channel = connection.channel() channel = connection.channel()