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 ...
#! /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)
# -----------------------------------------------
# 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 process_message(self, source, condition):
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 = gobject.Source()
io_source_server.set_callback(dialog.process_message)
io_source_server.add_poll(poll_fd_server)
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 [email protected]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/