-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
add default protocol_version #677
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,102 +1,103 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
|
||
|
||
from __future__ import division | ||
from __future__ import print_function | ||
|
||
import socketio | ||
|
||
import qlib | ||
from ..log import get_module_logger | ||
import pickle | ||
|
||
|
||
class Client: | ||
"""A client class | ||
|
||
Provide the connection tool functions for ClientProvider. | ||
""" | ||
|
||
def __init__(self, host, port): | ||
super(Client, self).__init__() | ||
self.sio = socketio.Client() | ||
self.server_host = host | ||
self.server_port = port | ||
self.logger = get_module_logger(self.__class__.__name__) | ||
# bind connect/disconnect callbacks | ||
self.sio.on( | ||
"connect", | ||
lambda: self.logger.debug("Connect to server {}".format(self.sio.connection_url)), | ||
) | ||
self.sio.on("disconnect", lambda: self.logger.debug("Disconnect from server!")) | ||
|
||
def connect_server(self): | ||
"""Connect to server.""" | ||
try: | ||
self.sio.connect("ws://" + self.server_host + ":" + str(self.server_port)) | ||
except socketio.exceptions.ConnectionError: | ||
self.logger.error("Cannot connect to server - check your network or server status") | ||
|
||
def disconnect(self): | ||
"""Disconnect from server.""" | ||
try: | ||
self.sio.eio.disconnect(True) | ||
except Exception as e: | ||
self.logger.error("Cannot disconnect from server : %s" % e) | ||
|
||
def send_request(self, request_type, request_content, msg_queue, msg_proc_func=None): | ||
"""Send a certain request to server. | ||
|
||
Parameters | ||
---------- | ||
request_type : str | ||
type of proposed request, 'calendar'/'instrument'/'feature'. | ||
request_content : dict | ||
records the information of the request. | ||
msg_proc_func : func | ||
the function to process the message when receiving response, should have arg `*args`. | ||
msg_queue: Queue | ||
The queue to pass the messsage after callback. | ||
""" | ||
head_info = {"version": qlib.__version__} | ||
|
||
def request_callback(*args): | ||
"""callback_wrapper | ||
|
||
:param *args: args[0] is the response content | ||
""" | ||
# args[0] is the response content | ||
self.logger.debug("receive data and enter queue") | ||
msg = dict(args[0]) | ||
if msg["detailed_info"] is not None: | ||
if msg["status"] != 0: | ||
self.logger.error(msg["detailed_info"]) | ||
else: | ||
self.logger.info(msg["detailed_info"]) | ||
if msg["status"] != 0: | ||
ex = ValueError(f"Bad response(status=={msg['status']}), detailed info: {msg['detailed_info']}") | ||
msg_queue.put(ex) | ||
else: | ||
if msg_proc_func is not None: | ||
try: | ||
ret = msg_proc_func(msg["result"]) | ||
except Exception as e: | ||
self.logger.exception("Error when processing message.") | ||
ret = e | ||
else: | ||
ret = msg["result"] | ||
msg_queue.put(ret) | ||
self.disconnect() | ||
self.logger.debug("disconnected") | ||
|
||
self.logger.debug("try connecting") | ||
self.connect_server() | ||
self.logger.debug("connected") | ||
# The pickle is for passing some parameters with special type(such as | ||
# pd.Timestamp) | ||
request_content = {"head": head_info, "body": pickle.dumps(request_content)} | ||
self.sio.on(request_type + "_response", request_callback) | ||
self.logger.debug("try sending") | ||
self.sio.emit(request_type + "_request", request_content) | ||
self.sio.wait() | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
|
||
|
||
from __future__ import division | ||
from __future__ import print_function | ||
|
||
import socketio | ||
|
||
import qlib | ||
from ..config import C | ||
from ..log import get_module_logger | ||
import pickle | ||
|
||
|
||
class Client: | ||
"""A client class | ||
|
||
Provide the connection tool functions for ClientProvider. | ||
""" | ||
|
||
def __init__(self, host, port): | ||
super(Client, self).__init__() | ||
self.sio = socketio.Client() | ||
self.server_host = host | ||
self.server_port = port | ||
self.logger = get_module_logger(self.__class__.__name__) | ||
# bind connect/disconnect callbacks | ||
self.sio.on( | ||
"connect", | ||
lambda: self.logger.debug("Connect to server {}".format(self.sio.connection_url)), | ||
) | ||
self.sio.on("disconnect", lambda: self.logger.debug("Disconnect from server!")) | ||
|
||
def connect_server(self): | ||
"""Connect to server.""" | ||
try: | ||
self.sio.connect("ws://" + self.server_host + ":" + str(self.server_port)) | ||
except socketio.exceptions.ConnectionError: | ||
self.logger.error("Cannot connect to server - check your network or server status") | ||
|
||
def disconnect(self): | ||
"""Disconnect from server.""" | ||
try: | ||
self.sio.eio.disconnect(True) | ||
except Exception as e: | ||
self.logger.error("Cannot disconnect from server : %s" % e) | ||
|
||
def send_request(self, request_type, request_content, msg_queue, msg_proc_func=None): | ||
"""Send a certain request to server. | ||
|
||
Parameters | ||
---------- | ||
request_type : str | ||
type of proposed request, 'calendar'/'instrument'/'feature'. | ||
request_content : dict | ||
records the information of the request. | ||
msg_proc_func : func | ||
the function to process the message when receiving response, should have arg `*args`. | ||
msg_queue: Queue | ||
The queue to pass the messsage after callback. | ||
""" | ||
head_info = {"version": qlib.__version__} | ||
|
||
def request_callback(*args): | ||
"""callback_wrapper | ||
|
||
:param *args: args[0] is the response content | ||
""" | ||
# args[0] is the response content | ||
self.logger.debug("receive data and enter queue") | ||
msg = dict(args[0]) | ||
if msg["detailed_info"] is not None: | ||
if msg["status"] != 0: | ||
self.logger.error(msg["detailed_info"]) | ||
else: | ||
self.logger.info(msg["detailed_info"]) | ||
if msg["status"] != 0: | ||
ex = ValueError(f"Bad response(status=={msg['status']}), detailed info: {msg['detailed_info']}") | ||
msg_queue.put(ex) | ||
else: | ||
if msg_proc_func is not None: | ||
try: | ||
ret = msg_proc_func(msg["result"]) | ||
except Exception as e: | ||
self.logger.exception("Error when processing message.") | ||
ret = e | ||
else: | ||
ret = msg["result"] | ||
msg_queue.put(ret) | ||
self.disconnect() | ||
self.logger.debug("disconnected") | ||
|
||
self.logger.debug("try connecting") | ||
self.connect_server() | ||
self.logger.debug("connected") | ||
# The pickle is for passing some parameters with special type(such as | ||
# pd.Timestamp) | ||
request_content = {"head": head_info, "body": pickle.dumps(request_content, protocol=C.dump_protocol_version)} | ||
self.sio.on(request_type + "_response", request_callback) | ||
self.logger.debug("try sending") | ||
self.sio.emit(request_type + "_request", request_content) | ||
self.sio.wait() |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The backend may not be a pickle.
Maybe you can add
Serializable.dump_kwargs
for dumping and encourage users to override it.