|
1 | | -# Copyright (c) Microsoft Corporation. |
2 | | -# Licensed under the MIT License. |
3 | | - |
4 | | - |
5 | | -from __future__ import division |
6 | | -from __future__ import print_function |
7 | | - |
8 | | -import socketio |
9 | | - |
10 | | -import qlib |
11 | | -from ..log import get_module_logger |
12 | | -import pickle |
13 | | - |
14 | | - |
15 | | -class Client: |
16 | | - """A client class |
17 | | -
|
18 | | - Provide the connection tool functions for ClientProvider. |
19 | | - """ |
20 | | - |
21 | | - def __init__(self, host, port): |
22 | | - super(Client, self).__init__() |
23 | | - self.sio = socketio.Client() |
24 | | - self.server_host = host |
25 | | - self.server_port = port |
26 | | - self.logger = get_module_logger(self.__class__.__name__) |
27 | | - # bind connect/disconnect callbacks |
28 | | - self.sio.on( |
29 | | - "connect", |
30 | | - lambda: self.logger.debug("Connect to server {}".format(self.sio.connection_url)), |
31 | | - ) |
32 | | - self.sio.on("disconnect", lambda: self.logger.debug("Disconnect from server!")) |
33 | | - |
34 | | - def connect_server(self): |
35 | | - """Connect to server.""" |
36 | | - try: |
37 | | - self.sio.connect("ws://" + self.server_host + ":" + str(self.server_port)) |
38 | | - except socketio.exceptions.ConnectionError: |
39 | | - self.logger.error("Cannot connect to server - check your network or server status") |
40 | | - |
41 | | - def disconnect(self): |
42 | | - """Disconnect from server.""" |
43 | | - try: |
44 | | - self.sio.eio.disconnect(True) |
45 | | - except Exception as e: |
46 | | - self.logger.error("Cannot disconnect from server : %s" % e) |
47 | | - |
48 | | - def send_request(self, request_type, request_content, msg_queue, msg_proc_func=None): |
49 | | - """Send a certain request to server. |
50 | | -
|
51 | | - Parameters |
52 | | - ---------- |
53 | | - request_type : str |
54 | | - type of proposed request, 'calendar'/'instrument'/'feature'. |
55 | | - request_content : dict |
56 | | - records the information of the request. |
57 | | - msg_proc_func : func |
58 | | - the function to process the message when receiving response, should have arg `*args`. |
59 | | - msg_queue: Queue |
60 | | - The queue to pass the messsage after callback. |
61 | | - """ |
62 | | - head_info = {"version": qlib.__version__} |
63 | | - |
64 | | - def request_callback(*args): |
65 | | - """callback_wrapper |
66 | | -
|
67 | | - :param *args: args[0] is the response content |
68 | | - """ |
69 | | - # args[0] is the response content |
70 | | - self.logger.debug("receive data and enter queue") |
71 | | - msg = dict(args[0]) |
72 | | - if msg["detailed_info"] is not None: |
73 | | - if msg["status"] != 0: |
74 | | - self.logger.error(msg["detailed_info"]) |
75 | | - else: |
76 | | - self.logger.info(msg["detailed_info"]) |
77 | | - if msg["status"] != 0: |
78 | | - ex = ValueError(f"Bad response(status=={msg['status']}), detailed info: {msg['detailed_info']}") |
79 | | - msg_queue.put(ex) |
80 | | - else: |
81 | | - if msg_proc_func is not None: |
82 | | - try: |
83 | | - ret = msg_proc_func(msg["result"]) |
84 | | - except Exception as e: |
85 | | - self.logger.exception("Error when processing message.") |
86 | | - ret = e |
87 | | - else: |
88 | | - ret = msg["result"] |
89 | | - msg_queue.put(ret) |
90 | | - self.disconnect() |
91 | | - self.logger.debug("disconnected") |
92 | | - |
93 | | - self.logger.debug("try connecting") |
94 | | - self.connect_server() |
95 | | - self.logger.debug("connected") |
96 | | - # The pickle is for passing some parameters with special type(such as |
97 | | - # pd.Timestamp) |
98 | | - request_content = {"head": head_info, "body": pickle.dumps(request_content)} |
99 | | - self.sio.on(request_type + "_response", request_callback) |
100 | | - self.logger.debug("try sending") |
101 | | - self.sio.emit(request_type + "_request", request_content) |
102 | | - self.sio.wait() |
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | + |
| 5 | +from __future__ import division |
| 6 | +from __future__ import print_function |
| 7 | + |
| 8 | +import socketio |
| 9 | + |
| 10 | +import qlib |
| 11 | +from ..config import C |
| 12 | +from ..log import get_module_logger |
| 13 | +import pickle |
| 14 | + |
| 15 | + |
| 16 | +class Client: |
| 17 | + """A client class |
| 18 | +
|
| 19 | + Provide the connection tool functions for ClientProvider. |
| 20 | + """ |
| 21 | + |
| 22 | + def __init__(self, host, port): |
| 23 | + super(Client, self).__init__() |
| 24 | + self.sio = socketio.Client() |
| 25 | + self.server_host = host |
| 26 | + self.server_port = port |
| 27 | + self.logger = get_module_logger(self.__class__.__name__) |
| 28 | + # bind connect/disconnect callbacks |
| 29 | + self.sio.on( |
| 30 | + "connect", |
| 31 | + lambda: self.logger.debug("Connect to server {}".format(self.sio.connection_url)), |
| 32 | + ) |
| 33 | + self.sio.on("disconnect", lambda: self.logger.debug("Disconnect from server!")) |
| 34 | + |
| 35 | + def connect_server(self): |
| 36 | + """Connect to server.""" |
| 37 | + try: |
| 38 | + self.sio.connect("ws://" + self.server_host + ":" + str(self.server_port)) |
| 39 | + except socketio.exceptions.ConnectionError: |
| 40 | + self.logger.error("Cannot connect to server - check your network or server status") |
| 41 | + |
| 42 | + def disconnect(self): |
| 43 | + """Disconnect from server.""" |
| 44 | + try: |
| 45 | + self.sio.eio.disconnect(True) |
| 46 | + except Exception as e: |
| 47 | + self.logger.error("Cannot disconnect from server : %s" % e) |
| 48 | + |
| 49 | + def send_request(self, request_type, request_content, msg_queue, msg_proc_func=None): |
| 50 | + """Send a certain request to server. |
| 51 | +
|
| 52 | + Parameters |
| 53 | + ---------- |
| 54 | + request_type : str |
| 55 | + type of proposed request, 'calendar'/'instrument'/'feature'. |
| 56 | + request_content : dict |
| 57 | + records the information of the request. |
| 58 | + msg_proc_func : func |
| 59 | + the function to process the message when receiving response, should have arg `*args`. |
| 60 | + msg_queue: Queue |
| 61 | + The queue to pass the messsage after callback. |
| 62 | + """ |
| 63 | + head_info = {"version": qlib.__version__} |
| 64 | + |
| 65 | + def request_callback(*args): |
| 66 | + """callback_wrapper |
| 67 | +
|
| 68 | + :param *args: args[0] is the response content |
| 69 | + """ |
| 70 | + # args[0] is the response content |
| 71 | + self.logger.debug("receive data and enter queue") |
| 72 | + msg = dict(args[0]) |
| 73 | + if msg["detailed_info"] is not None: |
| 74 | + if msg["status"] != 0: |
| 75 | + self.logger.error(msg["detailed_info"]) |
| 76 | + else: |
| 77 | + self.logger.info(msg["detailed_info"]) |
| 78 | + if msg["status"] != 0: |
| 79 | + ex = ValueError(f"Bad response(status=={msg['status']}), detailed info: {msg['detailed_info']}") |
| 80 | + msg_queue.put(ex) |
| 81 | + else: |
| 82 | + if msg_proc_func is not None: |
| 83 | + try: |
| 84 | + ret = msg_proc_func(msg["result"]) |
| 85 | + except Exception as e: |
| 86 | + self.logger.exception("Error when processing message.") |
| 87 | + ret = e |
| 88 | + else: |
| 89 | + ret = msg["result"] |
| 90 | + msg_queue.put(ret) |
| 91 | + self.disconnect() |
| 92 | + self.logger.debug("disconnected") |
| 93 | + |
| 94 | + self.logger.debug("try connecting") |
| 95 | + self.connect_server() |
| 96 | + self.logger.debug("connected") |
| 97 | + # The pickle is for passing some parameters with special type(such as |
| 98 | + # pd.Timestamp) |
| 99 | + request_content = {"head": head_info, "body": pickle.dumps(request_content, protocol=C.dump_protocol_version)} |
| 100 | + self.sio.on(request_type + "_response", request_callback) |
| 101 | + self.logger.debug("try sending") |
| 102 | + self.sio.emit(request_type + "_request", request_content) |
| 103 | + self.sio.wait() |
0 commit comments