-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mymdc proxy #226
Mymdc proxy #226
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,30 @@ | ||
import pickle | ||
import logging | ||
|
||
from kafka import KafkaConsumer | ||
from kafka import KafkaConsumer, KafkaProducer | ||
from PyQt5 import QtCore | ||
|
||
from ..backend.db import MsgKind, msg_dict | ||
from ..definitions import UPDATE_BROKERS, UPDATE_TOPIC | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class UpdateReceiver(QtCore.QObject): | ||
class UpdateAgent(QtCore.QObject): | ||
message = QtCore.pyqtSignal(object) | ||
|
||
def __init__(self, db_id: str) -> None: | ||
QtCore.QObject.__init__(self) | ||
self.update_topic = UPDATE_TOPIC.format(db_id) | ||
|
||
self.kafka_cns = KafkaConsumer( | ||
UPDATE_TOPIC.format(db_id), bootstrap_servers=UPDATE_BROKERS | ||
self.update_topic, bootstrap_servers=UPDATE_BROKERS | ||
) | ||
self.kafka_prd = KafkaProducer(bootstrap_servers=UPDATE_BROKERS, | ||
value_serializer=lambda d: pickle.dumps(d)) | ||
self.running = False | ||
|
||
def loop(self) -> None: | ||
def listen_loop(self) -> None: | ||
self.running = True | ||
|
||
while self.running: | ||
|
@@ -38,12 +42,38 @@ def loop(self) -> None: | |
|
||
self.message.emit(unpickled_msg) | ||
|
||
def run_values_updated(self, proposal, run, name, value): | ||
message = msg_dict(MsgKind.run_values_updated, | ||
{ | ||
"proposal": proposal, | ||
"run": run, | ||
"values": { | ||
name: value | ||
} | ||
}) | ||
|
||
# Note: the send() function returns a future that we don't await | ||
# immediately, but we call kafka_prd.flush() in stop() which will ensure | ||
# that all messages are sent. | ||
self.kafka_prd.send(self.update_topic, message) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it's a concern, I guess one option would be to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea, I added a call to |
||
|
||
def variable_set(self, name, title, description, variable_type): | ||
message = msg_dict(MsgKind.variable_set, | ||
{ | ||
"name": name, | ||
"title": title, | ||
"attributes": None, | ||
"type": variable_type | ||
}) | ||
self.kafka_prd.send(self.update_topic, message) | ||
|
||
def stop(self): | ||
self.running = False | ||
self.kafka_prd.flush(timeout=10) | ||
|
||
|
||
if __name__ == "__main__": | ||
recevier = UpdateReceiver("tcp://localhost:5556") | ||
monitor = UpdateAgent("tcp://localhost:5556") | ||
|
||
for record in recevier.kafka_cns: | ||
for record in monitor.kafka_cns: | ||
print(record.value.decode()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With how the cache is used (keys as a tuple of arguments basically as far as I could tell?) it seems pretty similar to what using the functools
cache
decorator would do right?e.g.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true, but when we make this public the caching should probably be disabled by default (at least until mymdc implements caching headers), so I'll stick with the dictionary for now so we can add an option to disable caching later.