##// END OF EJS Templates
Backport PR #5669: allows threadsafe sys.stdout.flush from background threads...
MinRK -
Show More
@@ -1,12 +1,7 b''
1 """wrappers for stdout/stderr forwarding over zmq
1 """Wrappers for forwarding stdout/stderr over zmq"""
2 """
3
2
4 #-----------------------------------------------------------------------------
3 # Copyright (c) IPython Development Team.
5 # Copyright (C) 2013 The IPython Development Team
4 # Distributed under the terms of the Modified BSD License.
6 #
7 # Distributed under the terms of the BSD License. The full license is in
8 # the file COPYING, distributed as part of this software.
9 #-----------------------------------------------------------------------------
10
5
11 import os
6 import os
12 import threading
7 import threading
@@ -15,6 +10,7 b' import uuid'
15 from io import StringIO, UnsupportedOperation
10 from io import StringIO, UnsupportedOperation
16
11
17 import zmq
12 import zmq
13 from zmq.eventloop.ioloop import IOLoop
18
14
19 from .session import extract_header
15 from .session import extract_header
20
16
@@ -130,6 +126,17 b' class OutStream(object):'
130 else:
126 else:
131 break
127 break
132
128
129 def _schedule_flush(self):
130 """schedule a flush in the main thread
131
132 only works with a tornado/pyzmq eventloop running
133 """
134 if IOLoop.initialized():
135 IOLoop.instance().add_callback(self.flush)
136 else:
137 # no async loop, at least force the timer
138 self._start = 0
139
133 def flush(self):
140 def flush(self):
134 """trigger actual zmq send"""
141 """trigger actual zmq send"""
135 if self.pub_socket is None:
142 if self.pub_socket is None:
@@ -140,9 +147,9 b' class OutStream(object):'
140 if mp_mode != CHILD:
147 if mp_mode != CHILD:
141 # we are master
148 # we are master
142 if not self._is_master_thread():
149 if not self._is_master_thread():
143 # sub-threads must not trigger flush,
150 # sub-threads must not trigger flush directly,
144 # but at least they can force the timer.
151 # but at least they can schedule an async flush, or force the timer.
145 self._start = 0
152 self._schedule_flush()
146 return
153 return
147
154
148 self._flush_from_subprocesses()
155 self._flush_from_subprocesses()
General Comments 0
You need to be logged in to leave comments. Login now