1
0
mirror of https://gitlab.com/libvirt/libvirt-python.git synced 2025-08-01 00:21:59 +03:00

event-test: rename example event loop impl

Use the name 'Poll' instead of 'Pure' for the event loop demo,
since there's now a second pure python loop impl available.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrange
2017-04-04 14:54:40 +01:00
parent 4cb0508048
commit 1c6503b800

View File

@ -39,10 +39,10 @@ def debug(msg):
# #
# It is a pure python implementation based around the poll() API # It is a pure python implementation based around the poll() API
# #
class virEventLoopPure: class virEventLoopPoll:
# This class contains the data we need to track for a # This class contains the data we need to track for a
# single file handle # single file handle
class virEventLoopPureHandle: class virEventLoopPollHandle:
def __init__(self, handle, fd, events, cb, opaque): def __init__(self, handle, fd, events, cb, opaque):
self.handle = handle self.handle = handle
self.fd = fd self.fd = fd
@ -70,7 +70,7 @@ class virEventLoopPure:
# This class contains the data we need to track for a # This class contains the data we need to track for a
# single periodic timer # single periodic timer
class virEventLoopPureTimer: class virEventLoopPollTimer:
def __init__(self, timer, interval, cb, opaque): def __init__(self, timer, interval, cb, opaque):
self.timer = timer self.timer = timer
self.interval = interval self.interval = interval
@ -142,14 +142,14 @@ class virEventLoopPure:
return next return next
# Lookup a virEventLoopPureHandle object based on file descriptor # Lookup a virEventLoopPollHandle object based on file descriptor
def get_handle_by_fd(self, fd): def get_handle_by_fd(self, fd):
for h in self.handles: for h in self.handles:
if h.get_fd() == fd: if h.get_fd() == fd:
return h return h
return None return None
# Lookup a virEventLoopPureHandle object based on its event loop ID # Lookup a virEventLoopPollHandle object based on its event loop ID
def get_handle_by_id(self, handleID): def get_handle_by_id(self, handleID):
for h in self.handles: for h in self.handles:
if h.get_id() == handleID: if h.get_id() == handleID:
@ -253,7 +253,7 @@ class virEventLoopPure:
handleID = self.nextHandleID + 1 handleID = self.nextHandleID + 1
self.nextHandleID = self.nextHandleID + 1 self.nextHandleID = self.nextHandleID + 1
h = self.virEventLoopPureHandle(handleID, fd, events, cb, opaque) h = self.virEventLoopPollHandle(handleID, fd, events, cb, opaque)
self.handles.append(h) self.handles.append(h)
self.poll.register(fd, self.events_to_poll(events)) self.poll.register(fd, self.events_to_poll(events))
@ -272,7 +272,7 @@ class virEventLoopPure:
timerID = self.nextTimerID + 1 timerID = self.nextTimerID + 1
self.nextTimerID = self.nextTimerID + 1 self.nextTimerID = self.nextTimerID + 1
h = self.virEventLoopPureTimer(timerID, interval, cb, opaque) h = self.virEventLoopPollTimer(timerID, interval, cb, opaque)
self.timers.append(h) self.timers.append(h)
self.interrupt() self.interrupt()
@ -361,7 +361,7 @@ class virEventLoopPure:
# This single global instance of the event loop wil be used for # This single global instance of the event loop wil be used for
# monitoring libvirt events # monitoring libvirt events
eventLoop = virEventLoopPure() eventLoop = virEventLoopPoll()
# This keeps track of what thread is running the event loop, # This keeps track of what thread is running the event loop,
# (if it is run in a background thread) # (if it is run in a background thread)
@ -371,7 +371,7 @@ eventLoopThread = None
# These next set of 6 methods are the glue between the official # These next set of 6 methods are the glue between the official
# libvirt events API, and our particular impl of the event loop # libvirt events API, and our particular impl of the event loop
# #
# There is no reason why the 'virEventLoopPure' has to be used. # There is no reason why the 'virEventLoopPoll' has to be used.
# An application could easily may these 6 glue methods hook into # An application could easily may these 6 glue methods hook into
# another event loop such as GLib's, or something like the python # another event loop such as GLib's, or something like the python
# Twisted event framework. # Twisted event framework.
@ -402,7 +402,7 @@ def virEventRemoveTimerImpl(timerID):
# This tells libvirt what event loop implementation it # This tells libvirt what event loop implementation it
# should use # should use
def virEventLoopPureRegister(): def virEventLoopPollRegister():
libvirt.virEventRegisterImpl(virEventAddHandleImpl, libvirt.virEventRegisterImpl(virEventAddHandleImpl,
virEventUpdateHandleImpl, virEventUpdateHandleImpl,
virEventRemoveHandleImpl, virEventRemoveHandleImpl,
@ -411,7 +411,7 @@ def virEventLoopPureRegister():
virEventRemoveTimerImpl) virEventRemoveTimerImpl)
# Directly run the event loop in the current thread # Directly run the event loop in the current thread
def virEventLoopPureRun(): def virEventLoopPollRun():
global eventLoop global eventLoop
eventLoop.run_loop() eventLoop.run_loop()
@ -420,10 +420,10 @@ def virEventLoopNativeRun():
libvirt.virEventRunDefaultImpl() libvirt.virEventRunDefaultImpl()
# Spawn a background thread to run the event loop # Spawn a background thread to run the event loop
def virEventLoopPureStart(): def virEventLoopPollStart():
global eventLoopThread global eventLoopThread
virEventLoopPureRegister() virEventLoopPollRegister()
eventLoopThread = threading.Thread(target=virEventLoopPureRun, name="libvirtEventLoop") eventLoopThread = threading.Thread(target=virEventLoopPollRun, name="libvirtEventLoop")
eventLoopThread.setDaemon(True) eventLoopThread.setDaemon(True)
eventLoopThread.start() eventLoopThread.start()
@ -684,7 +684,7 @@ def main():
# Run a background thread with the event loop # Run a background thread with the event loop
if use_pure_python_event_loop: if use_pure_python_event_loop:
virEventLoopPureStart() virEventLoopPollStart()
else: else:
virEventLoopNativeStart() virEventLoopNativeStart()