I've got it working! gobject.Source is not functional - you must derive your own class and define the functions prepare, check and dispatch. See attached example.
If this is actually intended, the error that's being raised should be a NotImplementedError. Btw, there doesn't seem to be any documentation around for the GSource/GPollFD wrappers. (Who cares, as long as it works - and pygobject/pygtk really rocks ;-) ) Regards, Martin Am Montag, den 29.06.2009, 22:59 +0200 schrieb Martin Schulze: > Sorry, it should read the "GSource/GPollFD wrappers", not "GIOSource > wrapper". Also, I should add that I'm using the Linux versions > 2.16.1/2.14.1 and the Windows versions 2.14.2/2.12.1 of pygobject/pygtk. > > Still, the problem remains unchanged ... > > Regards, > > Martin > > > > Am Sonntag, den 28.06.2009, 23:33 +0200 schrieb Martin Schulze: > > Hello, > > > > I have a problem with the GIOSource wrapper. In the attached test > > program I'm using either > > > > a) gobject.io_add_watch() or > > > > b) gobject.iosource.add_poll_fd() and gobject.iosource.attach() > > > > to listen on a pipe in the glib main context with the following result: > > > > a) works like a charm (see the dialog being updated every second). > > > > b) just produces the endlessly repeated error output > > > > AttributeError: prepare > > AttributeError: check > > > > Use "use_poll_fd={False/True}" to switch between a) and b). > > Can someone guess what's going wrong? > > > > Best Regards, > > > > Martin > > > > > > P.S.: Background: version b) helps me to port the program to win32. I > > have already implemented an overlapping i/o version of > > _multiprocessing.PipeConnection (in python using pywin32). I just need > > to hook the win32 event handle into the glib main context like in > > version b) of attached code ... > > > > _______________________________________________ > > pygtk mailing list pygtk@daa.com.au > > http://www.daa.com.au/mailman/listinfo/pygtk > > Read the PyGTK FAQ: http://faq.pygtk.org/ > > _______________________________________________ > pygtk mailing list pygtk@daa.com.au > http://www.daa.com.au/mailman/listinfo/pygtk > Read the PyGTK FAQ: http://faq.pygtk.org/
#! /usr/bin/env python import time import multiprocessing import gtk import glib import gobject # ----------------------------------------------- # background process # ----------------------------------------------- def client (conn_client): while True: if conn_client.poll(1): break t = time.localtime() msg = str(t.tm_year) + '-' + str(t.tm_mon) + '-' + str(t.tm_mday) + ', ' + str(t.tm_hour) + ':' + str(t.tm_min) + ':' + str(t.tm_sec) conn_client.send_bytes(msg) # ----------------------------------------------- # SourceFD # ----------------------------------------------- class SourceFD (gobject.Source): def __init__(self, fd, callback, check): gobject.Source.__init__(self) self.add_poll(fd) self.fd = fd self.__callback = callback self.__check = check def prepare(self): return ((self.__check)(), -1) def check(self): return (self.__check)() def dispatch(self, *args, **kw): return (self.__callback)(*args, **kw) # ----------------------------------------------- # dialog # ----------------------------------------------- class ServerDialog (gtk.Dialog): def __init__(self, conn_server): gtk.Dialog.__init__(self, title="Hello World", buttons=(gtk.STOCK_CLOSE,gtk.RESPONSE_ACCEPT)) self.label = gtk.Label("Last Messages:\nN/A") self.get_content_area().pack_start(self.label) self.conn_server = conn_server def destroy(self): self.label = None self.conn_server = None gtk.Dialog.destroy(self) def check(self): return self.conn_server.poll(0) def process_message(self, *args, **kw): msg = self.conn_server.recv_bytes() self.label.set_text("Last Messages:\n" + msg) return True # ----------------------------------------------- # main program # ----------------------------------------------- if __name__ == '__main__': use_poll_fd = True # set to True to stimulate bug # prepare pipe conn_server, conn_client = multiprocessing.Pipe() # create dialog dialog = ServerDialog(conn_server) # attach a new io source to the main context dialog.conn_server = conn_server if not use_poll_fd: io_source_server = gobject.io_add_watch(conn_server.fileno(), gobject.IO_IN, dialog.process_message) else: poll_fd_server = gobject.PollFD(conn_server.fileno(), gobject.IO_IN) io_source_server = SourceFD(poll_fd_server, dialog.process_message, dialog.check) io_source_server.attach(glib.main_context_default()) # start background process client = multiprocessing.Process(target=client, args=(conn_client,)) client.start() del conn_client # run the gtk main event loop dialog.show_all() dialog.run() print "finished" # cleanup if not use_poll_fd: gobject.source_remove(io_source_server) del io_source_server else: io_source_server.destroy() del io_source_server del poll_fd_server conn_server.send_bytes("quit") client.join(1) del client del conn_server dialog.destroy() del dialog
_______________________________________________ pygtk mailing list pygtk@daa.com.au http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://faq.pygtk.org/