Revision dcd511c8 lib/utils.py

b/lib/utils.py
1497 1497
    return False
1498 1498

  
1499 1499

  
1500
def WaitForSocketCondition(sock, event, timeout):
1501
  """Waits for a condition to occur on the socket.
1502

  
1503
  @type sock: socket
1504
  @param sock: Wait for events on this socket
1505
  @type event: int
1506
  @param event: ORed condition (see select module)
1507
  @type timeout: float or None
1508
  @param timeout: Timeout in seconds
1509
  @rtype: int or None
1510
  @return: None for timeout, otherwise occured conditions
1511

  
1512
  """
1513
  check = (event | select.POLLPRI |
1514
           select.POLLNVAL | select.POLLHUP | select.POLLERR)
1515

  
1516
  if timeout is not None:
1517
    # Poller object expects milliseconds
1518
    timeout *= 1000
1519

  
1520
  poller = select.poll()
1521
  poller.register(sock, event)
1522
  try:
1523
    while True:
1524
      # TODO: If the main thread receives a signal and we have no timeout, we
1525
      # could wait forever. This should check a global "quit" flag or
1526
      # something every so often.
1527
      io_events = poller.poll(timeout)
1528
      if not io_events:
1529
        # Timeout
1530
        return None
1531
      for (_, evcond) in io_events:
1532
        if evcond & check:
1533
          return evcond
1534
  finally:
1535
    poller.unregister(sock)
1536

  
1537

  
1500 1538
def partition(seq, pred=bool): # # pylint: disable-msg=W0622
1501 1539
  "Partition a list in two, based on the given predicate"
1502 1540
  return (list(itertools.ifilter(pred, seq)),

Also available in: Unified diff