integrating SocketServer with Tkinter Event-Loop
Peter Funk
pf at artcom0.north.de
Mon Oct 18 13:00:17 EDT 1999
I want to write a Python application with a Tkinter based GUI which
serves incoming requests using a SocketServer in the same process at the
same time. I am trying to use the standard lib module 'asyncore' for
this purpose. Here is short outline of my code (as a working example):
-------------------------------
#!/usr/bin/env python
import SocketServer, asyncore
class AsyncTCPServer(asyncore.dispatcher):
def __init__(self, host_port, handler, *args, **kw):
self.socket_server = SocketServer.TCPServer(host_port, handler)
asyncore.dispatcher.__init__(self, self.socket_server.socket)
def handle_read(self):
self.socket_server.handle_request()
if __name__ == "__main__":
import SimpleHTTPServer
server = AsyncTCPServer(('', 8080), SimpleHTTPServer.SimpleHTTPRequestHandler)
#
import Tkinter; Tk = Tkinter; del Tkinter
class NanoGUI(Tk.Tk):
def __init__(self):
Tk.Tk.__init__(self)
Tk.Button(self, text="Quit server", command=self.quit).pack()
self.socket_poll()
def socket_poll(self):
asyncore.poll(timeout=0.0)
self.after(100, self.socket_poll)
gui = NanoGUI()
gui.mainloop()
-------------------------------
The tricky part is the 'socket_poll' method, which does some kind of
"macro" busy waiting, while the GUI is idle.
I would like to here your comments about this solution.
With kind regards, Peter Funk
--
Peter Funk, Oldenburger Str.86, D-27777 Ganderkesee, Germany, Fax:+49 4222950260
office: +49 421 20419-0 (ArtCom GmbH, Grazer Str.8, D-28359 Bremen)
More information about the Python-list
mailing list