using sockets in wxPython

When I'm in the "MainLoop()" of a wxApp instance, how does
wxpython tell me something is availale on a socket?

I found the wxSocketClient object but this message
http://lists.wxwidgets.org/archive/wxPython-users/msg11926.html
says that wxSocketClient isn't implemented in wxPython.

Further, I'm running off of Debian and I can't find wxPython in here:
/usr/lib/python2.3/site-packages/wx-2.5.3-gtk2-unicode/wxPython/_core.py
so I suspect that wxPython doesn't handle wxSocketClient.

I see that Tkinter supports supports sockets with:

    from Tkinter import tkinter
    tkinter.createfilehandler(file, mask, callback)

but I'm loathe to use Tkinter. A reference is here at this webpage
http://mail.python.org/pipermail/python-announce-list/2003-August/002501.html

At best, the only answer seems to do polling by setting up a timer
but I can't believe this is the suggested approach.

Any help would be appreciated ... especially any code fragments :slight_smile:

Python has it's own socket library, so there isn't a need the wxWidgets portation.

Depending on what your needs are you might want to look at twisted.

Twisted-Python@twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

-Joe

Bearish wrote:

···

When I'm in the "MainLoop()" of a wxApp instance, how does
wxpython tell me something is availale on a socket?

I found the wxSocketClient object but this message
http://lists.wxwidgets.org/archive/wxPython-users/msg11926.html
says that wxSocketClient isn't implemented in wxPython.

Further, I'm running off of Debian and I can't find wxPython in here:
/usr/lib/python2.3/site-packages/wx-2.5.3-gtk2-unicode/wxPython/_core.py
so I suspect that wxPython doesn't handle wxSocketClient.

I see that Tkinter supports supports sockets with:

   from Tkinter import tkinter
   tkinter.createfilehandler(file, mask, callback)

but I'm loathe to use Tkinter. A reference is here at this webpage
GUI FAQ

At best, the only answer seems to do polling by setting up a timer
but I can't believe this is the suggested approach.

Any help would be appreciated ... especially any code fragments :slight_smile:

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

Joe Brown wrote:

Python has it's own socket library, so there isn't a need the wxWidgets portation.

Depending on what your needs are you might want to look at twisted.

... but if you are going to use Twisted, you will
want to run it in a separate thread from the wx event
loop -- the two don't play nice.

- Steve

Stephen Waterbury <golux@comcast.net> writes:

Joe Brown wrote:

Python has it's own socket library, so there isn't a need the
wxWidgets portation.
Depending on what your needs are you might want to look at twisted.

... but if you are going to use Twisted, you will
want to run it in a separate thread from the wx event
loop -- the two don't play nice.

As Stephen alludes to, there _is_ a need for the wxWidgets port
because the MainLoop method of wxApp blocks.
The problem isn't with Python's socket library,
the problem is with wxApp; either I have to mess around
with timers for polling or mess around with threads.

Tkinter's approach (at least for sockets) makes so much more sense
because input coming from a socket (or any file descriptor)
_is_ another event when execution is trapped in a wxApp MainLoop.

With threading, extra hassles crop up such as locks to protect data.
If wxPython has a user-triggered event, then a seperate thread
could have the data in an indeterminate state. At least with timers,
the events will be properly _serialized_. But with timers,
there is the inefficiency of polling.

Essentially, I have to use either the kludge of threads or the kludge of
polling timers when, in contrast, wxSockets gives clean, elegant
serialization of _both_ user events and file/socket events.
After all, there is a reason that this functionality
was built into the X Window System

Besides, why does wxSockets have to work at cross-purposes
to the existing socket library?
That is, wxSockets should be a wrapper _around_ Python's socket
library instead of some entirely disjoint object.
In particular, wxSocket methods should include, as a parameter,
a Python socket object; or a socket object should be a field
within a wxSocket object.

Tsk, this omission from wxPython is strange given
it's apparent superiority to Tkinter in most other ways.

...

Actually, I just had an idea which should be reasonably clean
(even if not as clean as the correct approach of wxSocket).
Have one thread block on a socket and, then, trigger an event
in the thread with the wxApp MainLoop. Is it possible to trigger
some kind of wxPython notification events across threads?

Bearish wrote:

Actually, I just had an idea which should be reasonably clean
(even if not as clean as the correct approach of wxSocket).
Have one thread block on a socket and, then, trigger an event
in the thread with the wxApp MainLoop. Is it possible to trigger
some kind of wxPython notification events across threads?

Yes. In fact I was going to suggest that you do exactly this. You could even go as far as making something that matches the wxSocket API. (In fact, IIRC some of the platforms actually do this to implement the wxSocket API for C++.)

Take a look at the Threads sample in the demo for an example of sending events from worker threads. You can use wx.PostEvent to do so in a thread-safe way. There is also a helper function that will let you schedule a call to a GUI function or method in the GUI thread from any other thread, wx.CallAfter.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!