Start a data processing server. This command starts a server in the current process that performs the actual data processing (by retrieving data from the given data stream). It also starts a second process, the broker, which mediates between the server and the client. The broker also keeps
(data_stream, port=5557, hwm=20)
| 118 | |
| 119 | |
| 120 | def start_server(data_stream, port=5557, hwm=20): |
| 121 | """Start a data processing server. |
| 122 | |
| 123 | This command starts a server in the current process that performs the |
| 124 | actual data processing (by retrieving data from the given data stream). |
| 125 | It also starts a second process, the broker, which mediates between the |
| 126 | server and the client. The broker also keeps a buffer of batches in |
| 127 | memory. |
| 128 | |
| 129 | Parameters |
| 130 | ---------- |
| 131 | data_stream : generator |
| 132 | The data stream to return examples from. |
| 133 | port : int, optional |
| 134 | The port the server and the client (training loop) will use to |
| 135 | communicate. Defaults to 5557. |
| 136 | hwm : int, optional |
| 137 | The `ZeroMQ high-water mark (HWM) |
| 138 | <http://zguide.zeromq.org/page:all#High-Water-Marks>`_ on the |
| 139 | sending socket. Increasing this increases the buffer, which can be |
| 140 | useful if your data preprocessing times are very random. However, |
| 141 | it will increase memory usage. There is no easy way to tell how |
| 142 | many batches will actually be queued with a particular HWM. |
| 143 | Defaults to 10. Be sure to set the corresponding HWM on the |
| 144 | receiving end as well. |
| 145 | """ |
| 146 | logging.basicConfig(level='INFO') |
| 147 | |
| 148 | context = zmq.Context() |
| 149 | socket = context.socket(zmq.PUSH) |
| 150 | socket.set_hwm(hwm) |
| 151 | socket.bind('tcp://*:{}'.format(port)) |
| 152 | |
| 153 | # it = itertools.tee(data_stream) |
| 154 | it = data_stream |
| 155 | |
| 156 | logger.info('server started') |
| 157 | while True: |
| 158 | try: |
| 159 | data = next(it) |
| 160 | stop = False |
| 161 | logger.debug("sending {} arrays".format(len(data))) |
| 162 | except StopIteration: |
| 163 | it = data_stream |
| 164 | data = None |
| 165 | stop = True |
| 166 | logger.debug("sending StopIteration") |
| 167 | send_arrays(socket, data, stop=stop) |
| 168 | |
| 169 | # Example |
| 170 | if __name__ == "__main__": |