Revision dcd511c8 lib/http/__init__.py

b/lib/http/__init__.py
311 311
    return serializer.LoadJson(data)
312 312

  
313 313

  
314
def WaitForSocketCondition(sock, event, timeout):
315
  """Waits for a condition to occur on the socket.
316

  
317
  @type sock: socket
318
  @param sock: Wait for events on this socket
319
  @type event: int
320
  @param event: ORed condition (see select module)
321
  @type timeout: float or None
322
  @param timeout: Timeout in seconds
323
  @rtype: int or None
324
  @return: None for timeout, otherwise occured conditions
325

  
326
  """
327
  check = (event | select.POLLPRI |
328
           select.POLLNVAL | select.POLLHUP | select.POLLERR)
329

  
330
  if timeout is not None:
331
    # Poller object expects milliseconds
332
    timeout *= 1000
333

  
334
  poller = select.poll()
335
  poller.register(sock, event)
336
  try:
337
    while True:
338
      # TODO: If the main thread receives a signal and we have no timeout, we
339
      # could wait forever. This should check a global "quit" flag or
340
      # something every so often.
341
      io_events = poller.poll(timeout)
342
      if not io_events:
343
        # Timeout
344
        return None
345
      for (_, evcond) in io_events:
346
        if evcond & check:
347
          return evcond
348
  finally:
349
    poller.unregister(sock)
350

  
351

  
352 314
def SocketOperation(sock, op, arg1, timeout):
353 315
  """Wrapper around socket functions.
354 316

  
......
399 361
      else:
400 362
        wait_for_event = event_poll
401 363

  
402
      event = WaitForSocketCondition(sock, wait_for_event, timeout)
364
      event = utils.WaitForSocketCondition(sock, wait_for_event, timeout)
403 365
      if event is None:
404 366
        raise HttpSocketTimeout()
405 367

  

Also available in: Unified diff