def _add_watch(self, obj):
mask = self._get_mask(obj)
if mask:
source_id = gobject.io_add_watch(obj, mask, self._handle_io,
priority=IO_PRIORITY)
self._watch_map[obj] = (source_id, mask)
return False
# This should be exceptional. The channel is still open, but is neither
# readable nor writable. Retry until it is, but with a timeout_add() to
# preserve CPU.
if self._watch_map[obj][1] == 'idle_add':
source_id = gobject.timeout_add(200, self._add_watch, obj,
priority=gobject.PRIORITY_LOW)
self._watch_map[obj] = (source_id, 'timeout_add')
return False
return True
python类io_add_watch()的实例源码
def _register_io_handlers(self):
def handler(source, cond):
if cond & gobject.IO_IN:
if not self.fifo:
self.fifo = open(self.fifo_name, "w")
self.fifo.write(source.recv(1024))
return True
else:
self.log.debug("Error communicating with MPlayer")
return False
flags = gobject.IO_IN | gobject.IO_ERR | gobject.IO_HUP
self.io_watcher_id = gobject.io_add_watch(self.data,
flags,
handler)
# _register_io_handlers()
def __init__(self, mapping=None, **kwargs):
# socket -> (source_id, mask) map, for io_add_watch and source_remove
self._watch_map = dict()
# fd -> socket map, for the interface
self._fd_map = dict()
if mapping is not None:
self.update(mapping)
if kwargs:
self.update(kwargs)
def __init__(self, useGtk=True):
self.context = gobject.main_context_default()
self.loop = gobject.MainLoop()
posixbase.PosixReactorBase.__init__(self)
# pre 2.3.91 the glib iteration and mainloop functions didn't release
# global interpreter lock, thus breaking thread and signal support.
if (hasattr(gobject, "pygtk_version") and gobject.pygtk_version >= (2, 3, 91)
and not useGtk):
self.__pending = self.context.pending
self.__iteration = self.context.iteration
self.__crash = self.loop.quit
self.__run = self.loop.run
else:
import gtk
self.__pending = gtk.events_pending
self.__iteration = gtk.main_iteration
self.__crash = _our_mainquit
self.__run = gtk.main
# The input_add function in pygtk1 checks for objects with a
# 'fileno' method and, if present, uses the result of that method
# as the input source. The pygtk2 input_add does not do this. The
# function below replicates the pygtk1 functionality.
# In addition, pygtk maps gtk.input_add to _gobject.io_add_watch, and
# g_io_add_watch() takes different condition bitfields than
# gtk_input_add(). We use g_io_add_watch() here in case pygtk fixes this
# bug.
def input_add(self, source, condition, callback):
if hasattr(source, 'fileno'):
# handle python objects
def wrapper(source, condition, real_s=source, real_cb=callback):
return real_cb(real_s, condition)
return gobject.io_add_watch(source.fileno(), condition, wrapper)
else:
return gobject.io_add_watch(source, condition, callback)
def _init_socket_handlers(self):
if self._socket_watch is not None:
gobject.source_remove(self._socket_watch)
self._socket_watch = gobject.io_add_watch(self._client.socket().fileno(), gobject.IO_IN,
self._on_socket_in)
if self._socket_timer is None:
self._socket_timer = gobject.timeout_add_seconds(1, exit_on_error, self._on_socket_timer)
def inputhook_gtk():
gobject.io_add_watch(sys.stdin, gobject.IO_IN, _main_quit)
gtk.main()
return 0
def inputhook(context):
"""
When the eventloop of prompt-toolkit is idle, call this inputhook.
This will run the GTK main loop until the file descriptor
`context.fileno()` becomes ready.
:param context: An `InputHookContext` instance.
"""
def _main_quit(*a, **kw):
gtk.main_quit()
return False
gobject.io_add_watch(context.fileno(), gobject.IO_IN, _main_quit)
gtk.main()
def __init__(self, useGtk=True):
self.context = gobject.main_context_default()
self.loop = gobject.MainLoop()
posixbase.PosixReactorBase.__init__(self)
# pre 2.3.91 the glib iteration and mainloop functions didn't release
# global interpreter lock, thus breaking thread and signal support.
if (hasattr(gobject, "pygtk_version") and gobject.pygtk_version >= (2, 3, 91)
and not useGtk):
self.__pending = self.context.pending
self.__iteration = self.context.iteration
self.__crash = self.loop.quit
self.__run = self.loop.run
else:
import gtk
self.__pending = gtk.events_pending
self.__iteration = gtk.main_iteration
self.__crash = _our_mainquit
self.__run = gtk.main
# The input_add function in pygtk1 checks for objects with a
# 'fileno' method and, if present, uses the result of that method
# as the input source. The pygtk2 input_add does not do this. The
# function below replicates the pygtk1 functionality.
# In addition, pygtk maps gtk.input_add to _gobject.io_add_watch, and
# g_io_add_watch() takes different condition bitfields than
# gtk_input_add(). We use g_io_add_watch() here in case pygtk fixes this
# bug.
def input_add(self, source, condition, callback):
if hasattr(source, 'fileno'):
# handle python objects
def wrapper(source, condition, real_s=source, real_cb=callback):
return real_cb(real_s, condition)
return gobject.io_add_watch(source.fileno(), condition, wrapper)
else:
return gobject.io_add_watch(source, condition, callback)
def inputhook_gtk():
gobject.io_add_watch(sys.stdin, gobject.IO_IN, _main_quit)
gtk.main()
return 0
def inputhook(context):
"""
When the eventloop of prompt-toolkit is idle, call this inputhook.
This will run the GTK main loop until the file descriptor
`context.fileno()` becomes ready.
:param context: An `InputHookContext` instance.
"""
def _main_quit(*a, **kw):
gtk.main_quit()
return False
gobject.io_add_watch(context.fileno(), gobject.IO_IN, _main_quit)
gtk.main()
def inputhook_gtk():
gobject.io_add_watch(sys.stdin, gobject.IO_IN, _main_quit)
gtk.main()
return 0
def inputhook(context):
"""
When the eventloop of prompt-toolkit is idle, call this inputhook.
This will run the GTK main loop until the file descriptor
`context.fileno()` becomes ready.
:param context: An `InputHookContext` instance.
"""
def _main_quit(*a, **kw):
gtk.main_quit()
return False
gobject.io_add_watch(context.fileno(), gobject.IO_IN, _main_quit)
gtk.main()
def inputhook_gtk():
gobject.io_add_watch(sys.stdin, gobject.IO_IN, _main_quit)
gtk.main()
return 0
def inputhook(context):
"""
When the eventloop of prompt-toolkit is idle, call this inputhook.
This will run the GTK main loop until the file descriptor
`context.fileno()` becomes ready.
:param context: An `InputHookContext` instance.
"""
def _main_quit(*a, **kw):
gtk.main_quit()
return False
gobject.io_add_watch(context.fileno(), gobject.IO_IN, _main_quit)
gtk.main()
def run(self):
gobject.io_add_watch(0, gobject.IO_IN | gobject.IO_HUP, self.__stdin_cb)
super(MainLoop, self).run()
def _register_io_handlers(self):
flags_err = gobject.IO_ERR | gobject.IO_HUP
def handler(fd, flags):
if flags & flags_err:
return False
else:
while self.playing:
line = self._readline()
if not line:
return True
elif line[:2] == "A:" and line[-1] =="\r":
pieces = line.split()
pos = float(pieces[1])
self.emit("pos", pos)
return True
elif line == "Exiting... (Quit)\n":
self.proc.wait()
self.proc = None
self.emit("eos")
self.emit("state-changed", PlayerEngine.STATE_NONE)
return False
elif line == "Starting playback...\n":
# make buffer empty
while line:
line = self._readline(100)
if line and line[:2] == "A:" and line[-1] == "\r":
break
self.set_volume(self.volume)
self.set_mute(self.mute)
self.set_fullscreen(self.fullscreen)
self._query_media_info()
self._register_state_timeout()
self.emit("state-changed", PlayerEngine.STATE_PLAYING)
return True
else:
self.log.debug("Ignored MPlayer output: %r" % line)
return True
flags = gobject.IO_IN | gobject.IO_PRI | flags_err
self.out_watcher = gobject.io_add_watch(self.proc.stdout,
flags, handler)
# _register_io_handlers()