|
12 | 12 | import warnings
|
13 | 13 | from collections.abc import Iterable
|
14 | 14 | from concurrent.futures import Future
|
| 15 | +from contextlib import contextmanager |
15 | 16 | from getpass import getpass
|
16 | 17 | from pprint import pprint
|
17 | 18 | from threading import current_thread
|
@@ -990,21 +991,39 @@ def _stop_io_thread(self):
|
990 | 991 | self._io_thread.join()
|
991 | 992 |
|
992 | 993 | def _setup_streams(self):
|
993 |
| - self._query_stream = ZMQStream(self._query_socket, self._io_loop) |
| 994 | + self._streams = [] |
| 995 | + self._query_stream = s = ZMQStream(self._query_socket, self._io_loop) |
| 996 | + self._streams.append(s) |
| 997 | + |
| 998 | + self._control_stream = s = ZMQStream(self._control_socket, self._io_loop) |
| 999 | + self._streams.append(s) |
| 1000 | + self._mux_stream = s = ZMQStream(self._mux_socket, self._io_loop) |
| 1001 | + self._streams.append(s) |
| 1002 | + self._task_stream = s = ZMQStream(self._task_socket, self._io_loop) |
| 1003 | + self._streams.append(s) |
| 1004 | + self._broadcast_stream = s = ZMQStream(self._broadcast_socket, self._io_loop) |
| 1005 | + self._streams.append(s) |
| 1006 | + self._iopub_stream = s = ZMQStream(self._iopub_socket, self._io_loop) |
| 1007 | + self._streams.append(s) |
| 1008 | + self._notification_stream = s = ZMQStream( |
| 1009 | + self._notification_socket, self._io_loop |
| 1010 | + ) |
| 1011 | + self._streams.append(s) |
| 1012 | + self._start_receiving() |
| 1013 | + |
| 1014 | + def _start_receiving(self): |
994 | 1015 | self._query_stream.on_recv(self._dispatch_single_reply, copy=False)
|
995 |
| - self._control_stream = ZMQStream(self._control_socket, self._io_loop) |
996 | 1016 | self._control_stream.on_recv(self._dispatch_single_reply, copy=False)
|
997 |
| - self._mux_stream = ZMQStream(self._mux_socket, self._io_loop) |
998 | 1017 | self._mux_stream.on_recv(self._dispatch_reply, copy=False)
|
999 |
| - self._task_stream = ZMQStream(self._task_socket, self._io_loop) |
1000 | 1018 | self._task_stream.on_recv(self._dispatch_reply, copy=False)
|
1001 |
| - self._iopub_stream = ZMQStream(self._iopub_socket, self._io_loop) |
| 1019 | + self._broadcast_stream.on_recv(self._dispatch_reply, copy=False) |
1002 | 1020 | self._iopub_stream.on_recv(self._dispatch_iopub, copy=False)
|
1003 |
| - self._notification_stream = ZMQStream(self._notification_socket, self._io_loop) |
1004 | 1021 | self._notification_stream.on_recv(self._dispatch_notification, copy=False)
|
1005 | 1022 |
|
1006 |
| - self._broadcast_stream = ZMQStream(self._broadcast_socket, self._io_loop) |
1007 |
| - self._broadcast_stream.on_recv(self._dispatch_reply, copy=False) |
| 1023 | + def _stop_receiving(self): |
| 1024 | + """Stop receiving on engine streams""" |
| 1025 | + for s in self._streams: |
| 1026 | + s.stop_on_recv() |
1008 | 1027 |
|
1009 | 1028 | def _start_io_thread(self):
|
1010 | 1029 | """Start IOLoop in a background thread."""
|
@@ -1034,6 +1053,30 @@ def _io_main(self, start_evt=None):
|
1034 | 1053 | self._io_loop.start()
|
1035 | 1054 | self._io_loop.close()
|
1036 | 1055 |
|
| 1056 | + @contextmanager |
| 1057 | + def _pause_results(self): |
| 1058 | + """Context manager to pause receiving results |
| 1059 | +
|
| 1060 | + When submitting lots of tasks, |
| 1061 | + the arrival of results can disrupt the processing |
| 1062 | + of new submissions. |
| 1063 | +
|
| 1064 | + Threadsafe. |
| 1065 | + """ |
| 1066 | + f = Future() |
| 1067 | + |
| 1068 | + def _stop(): |
| 1069 | + self._stop_receiving() |
| 1070 | + f.set_result(None) |
| 1071 | + |
| 1072 | + # use add_callback to make it threadsafe |
| 1073 | + self._io_loop.add_callback(_stop) |
| 1074 | + f.result() |
| 1075 | + try: |
| 1076 | + yield |
| 1077 | + finally: |
| 1078 | + self._io_loop.add_callback(self._start_receiving) |
| 1079 | + |
1037 | 1080 | @unpack_message
|
1038 | 1081 | def _dispatch_single_reply(self, msg):
|
1039 | 1082 | """Dispatch single (non-execution) replies"""
|
|
0 commit comments