Revision 1e063ccd lib/daemon.py

b/lib/daemon.py
24 24

  
25 25
import asyncore
26 26
import asynchat
27
import collections
27 28
import grp
28 29
import os
29 30
import pwd
......
199 200
    self.set_terminator(terminator)
200 201
    self.ibuffer = []
201 202
    self.next_incoming_message = 0
203
    self.oqueue = collections.deque()
202 204

  
203 205
  # this method is overriding an asynchat.async_chat method
204 206
  def collect_incoming_data(self, data):
......
225 227
    # TODO: move this method to raise NotImplementedError
226 228
    # raise NotImplementedError
227 229

  
230
  def send_message(self, message):
231
    """Send a message to the remote peer. This function is thread-safe.
232

  
233
    @type message: string
234
    @param message: message to send, without the terminator
235

  
236
    @warning: If calling this function from a thread different than the one
237
    performing the main asyncore loop, remember that you have to wake that one
238
    up.
239

  
240
    """
241
    # If we just append the message we received to the output queue, this
242
    # function can be safely called by multiple threads at the same time, and
243
    # we don't need locking, since deques are thread safe.
244
    self.oqueue.append(message)
245

  
246
  # this method is overriding an asyncore.dispatcher method
247
  def writable(self):
248
    # the output queue may become full just after we called writable. This only
249
    # works if we know we'll have something else waking us up from the select,
250
    # in such case, anyway.
251
    return asynchat.async_chat.writable(self) or self.oqueue
252

  
253
  # this method is overriding an asyncore.dispatcher method
254
  def handle_write(self):
255
    if self.oqueue:
256
      data = self.oqueue.popleft()
257
      self.push(data + self.terminator)
258
    self.initiate_send()
259

  
228 260
  def close_log(self):
229 261
    logging.info("Closing connection from %s",
230 262
                 FormatAddress(self.family, self.peer_address))

Also available in: Unified diff