diff options
author | Cole Robinson <crobinso@redhat.com> | 2011-01-05 16:47:22 -0500 |
---|---|---|
committer | Cole Robinson <crobinso@redhat.com> | 2011-01-10 14:44:04 -0500 |
commit | 8f3b6bc2ddb0b8154b0a11a94d6cbcccc460abc9 (patch) | |
tree | d32b10d5bace502ce7b57a312479d7f0d9096bf8 /examples | |
parent | remote: Don't lose track of events when callbacks are slow (diff) | |
download | libvirt-8f3b6bc2ddb0b8154b0a11a94d6cbcccc460abc9.tar.gz libvirt-8f3b6bc2ddb0b8154b0a11a94d6cbcccc460abc9.tar.bz2 libvirt-8f3b6bc2ddb0b8154b0a11a94d6cbcccc460abc9.zip |
event-test: Simplify debug on/off
Make it easy to change debugging if being used by a client program.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/domain-events/events-python/event-test.py | 37 |
1 files changed, 19 insertions, 18 deletions
diff --git a/examples/domain-events/events-python/event-test.py b/examples/domain-events/events-python/event-test.py index 7c5af68cd..903f934b2 100644 --- a/examples/domain-events/events-python/event-test.py +++ b/examples/domain-events/events-python/event-test.py @@ -15,6 +15,12 @@ import errno import time import threading +do_debug = False +def debug(msg): + global do_debug + if do_debug: + print msg + # # This general purpose event loop will support waiting for file handle # I/O and errors events, as well as scheduling repeatable timers with @@ -83,8 +89,7 @@ class virEventLoopPure: self.opaque[1]) - def __init__(self, debug=False): - self.debugOn = debug + def __init__(self): self.poll = select.poll() self.pipetrick = os.pipe() self.nextHandleID = 1 @@ -106,13 +111,9 @@ class virEventLoopPure: # with the event loop for input events. When we need to force # the main thread out of a poll() sleep, we simple write a # single byte of data to the other end of the pipe. - self.debug("Self pipe watch %d write %d" %(self.pipetrick[0], self.pipetrick[1])) + debug("Self pipe watch %d write %d" %(self.pipetrick[0], self.pipetrick[1])) self.poll.register(self.pipetrick[0], select.POLLIN) - def debug(self, msg): - if self.debugOn: - print msg - # Calculate when the next timeout is due to occurr, returning # the absolute timestamp for the next timeout, or 0 if there is @@ -166,7 +167,7 @@ class virEventLoopPure: def run_once(self): sleep = -1 next = self.next_timeout() - self.debug("Next timeout due at %d" % next) + debug("Next timeout due at %d" % next) if next > 0: now = int(time.time() * 1000) if now >= next: @@ -174,7 +175,7 @@ class virEventLoopPure: else: sleep = (next - now) / 1000.0 - self.debug("Poll with a sleep of %d" % sleep) + debug("Poll with a sleep of %d" % sleep) events = self.poll.poll(sleep) # Dispatch any file handle events that occurred @@ -188,7 +189,7 @@ class virEventLoopPure: h = self.get_handle_by_fd(fd) if h: - self.debug("Dispatch fd %d handle %d events %d" % (fd, h.get_id(), revents)) + debug("Dispatch fd %d handle %d events %d" % (fd, h.get_id(), revents)) h.dispatch(self.events_from_poll(revents)) now = int(time.time() * 1000) @@ -201,7 +202,7 @@ class virEventLoopPure: # Deduct 20ms, since schedular timeslice # means we could be ever so slightly early if now >= (want-20): - self.debug("Dispatch timer %d now %s want %s" % (t.get_id(), str(now), str(want))) + debug("Dispatch timer %d now %s want %s" % (t.get_id(), str(now), str(want))) t.set_last_fired(now) t.dispatch() @@ -230,7 +231,7 @@ class virEventLoopPure: self.poll.register(fd, self.events_to_poll(events)) self.interrupt() - self.debug("Add handle %d fd %d events %d" % (handleID, fd, events)) + debug("Add handle %d fd %d events %d" % (handleID, fd, events)) return handleID @@ -247,7 +248,7 @@ class virEventLoopPure: self.timers.append(h) self.interrupt() - self.debug("Add timer %d interval %d" % (timerID, interval)) + debug("Add timer %d interval %d" % (timerID, interval)) return timerID @@ -260,7 +261,7 @@ class virEventLoopPure: self.poll.register(h.get_fd(), self.events_to_poll(events)) self.interrupt() - self.debug("Update handle %d fd %d events %d" % (handleID, h.get_fd(), events)) + debug("Update handle %d fd %d events %d" % (handleID, h.get_fd(), events)) # Change the periodic frequency of the timer def update_timer(self, timerID, interval): @@ -269,7 +270,7 @@ class virEventLoopPure: h.set_interval(interval); self.interrupt() - self.debug("Update timer %d interval %d" % (timerID, interval)) + debug("Update timer %d interval %d" % (timerID, interval)) break # Stop monitoring for events on the file handle @@ -278,7 +279,7 @@ class virEventLoopPure: for h in self.handles: if h.get_id() == handleID: self.poll.unregister(h.get_fd()) - self.debug("Remove handle %d fd %d" % (handleID, h.get_fd())) + debug("Remove handle %d fd %d" % (handleID, h.get_fd())) else: handles.append(h) self.handles = handles @@ -290,7 +291,7 @@ class virEventLoopPure: for h in self.timers: if h.get_id() != timerID: timers.append(h) - self.debug("Remove timer %d" % timerID) + debug("Remove timer %d" % timerID) self.timers = timers self.interrupt() @@ -329,7 +330,7 @@ class virEventLoopPure: # This single global instance of the event loop wil be used for # monitoring libvirt events -eventLoop = virEventLoopPure(debug=False) +eventLoop = virEventLoopPure() # This keeps track of what thread is running the event loop, # (if it is run in a background thread) |