Revision 112d240d lib/daemon.py

b/lib/daemon.py
22 22
"""Module with helper classes and functions for daemons"""
23 23

  
24 24

  
25
import asyncore
25 26
import os
26 27
import select
27 28
import signal
......
40 41
    """Constructs a new Mainloop instance.
41 42

  
42 43
    """
43
    self._io_wait = {}
44 44
    self._signal_wait = []
45
    self._poller = select.poll()
46 45

  
47 46
  @utils.SignalHandled([signal.SIGCHLD])
48 47
  @utils.SignalHandled([signal.SIGTERM])
......
66 65
      if stop_on_empty and not (self._io_wait):
67 66
        break
68 67

  
69
      # Wait for I/O events
70
      try:
71
        io_events = self._poller.poll(None)
72
      except select.error, err:
73
        # EINTR can happen when signals are sent
74
        if err.args and err.args[0] in (errno.EINTR,):
75
          io_events = None
76
        else:
77
          raise
78

  
79
      if io_events:
80
        # Check for I/O events
81
        for (evfd, evcond) in io_events:
82
          owner = self._io_wait.get(evfd, None)
83
          if owner:
84
            owner.OnIO(evfd, evcond)
68
      asyncore.loop(timeout=5, count=1, use_poll=True)
85 69

  
86 70
      # Check whether a signal was raised
87 71
      for sig in signal_handlers:
......
101 85
    for owner in self._signal_wait:
102 86
      owner.OnSignal(signal.SIGCHLD)
103 87

  
104
  def RegisterIO(self, owner, fd, condition):
105
    """Registers a receiver for I/O notifications
106

  
107
    The receiver must support a "OnIO(self, fd, conditions)" function.
108

  
109
    @type owner: instance
110
    @param owner: Receiver
111
    @type fd: int
112
    @param fd: File descriptor
113
    @type condition: int
114
    @param condition: ORed field of conditions to be notified
115
                      (see select module)
116

  
117
    """
118
    # select.Poller also supports file() like objects, but we don't.
119
    assert isinstance(fd, (int, long)), \
120
      "Only integers are supported for file descriptors"
121

  
122
    self._io_wait[fd] = owner
123
    self._poller.register(fd, condition)
124

  
125 88
  def RegisterSignal(self, owner):
126 89
    """Registers a receiver for signal notifications
127 90

  

Also available in: Unified diff