asynchronous comunication, wxPython and threads.
Peter Hansen
peter at engcorp.com
Tue Jun 21 11:07:35 EDT 2005
Zunbeltz Izaola wrote:
> I'm developing a GUI program (wxPython). This program has to comunicate
> (TCP) whit other program that controls a laboratory machine to do a
> measurement. I have a dialog box, wiht two buttoms "Start measurement" and
> "Stop". "Start" executes a function that do the measurement in the
> following way.
>
> 1) My program send a socket.
Please clarify: what does this mean? "Sending a socket" is not a usual
way to describe TCP communications. Do you mean your program _opens_ a
socket (like a phone connection) and _sends_ some data, then waits for
data to be received from the other end? Or are you (as it almost
sounds) opening and closing sockets repeatedly for each part of the
conversation?
> 2) The other program recives it an send confirmation.
> 3) My program waits for the confirmation and send the next when
> confirmation received.
>
> This comunication is done in a new thread not to frezee the GUI.
> The problem is that when "Stop" is done (it kills the thread) some
> confirmation sockets are mixed (are not receibed in the correct order
> although i use tcp).
I think you are using the term "socket" where you should be using
"packet". A socket is the virtual connection created by TCP. A packet
is either a single blob of data sent by the TCP code in the operating
system, or perhaps a single chunk of your own data.
If you are using a single TCP socket to send multiple packets, and you
are talking about those packets being sent out of order, it's very
unlikely and there must be another explanation. TCP _is_ reliable, and
you will not get data out of order unless you do something to screw
things up, for example by creating a race condition by doing
multithreaded code incorrectly.
> I have been told not to do comunication in a new thread; instead, I should
> use asyncronus comunication.
Some people advise that, but there's really nothing *wrong* with doing
this in a second thread, and in fact I do similar things all the time
with no ill effects. While async frameworks _can_ make this easier,
they could also make it harder (at least for a while) as you adjust your
brain to the new approach. Furthermore, at least in the case of
wxPython and Twisted (on Windows) there can be problems integrating the
two loops. I don't believe the latest Twisted claims to have fully
solved the problems involved yet, so you might still be required to have
a second thread for the TCP stuff.
> What module should i use, asyncore, asynchat, twisted,(anohter)
> How can i implement this asynchronous comunication and the ability to
> stop this long run funciton (the measurement can take hours or days, so
> i need a way to stop it)?
I use a non-blocking socket and select() calls in my thread, and
communicate with the GUI thread using appropriate Queue objects and
calls to PostEvent() (or CallAfter()) on the wx side of things. It's
pretty straightforward, so if you post a small piece of your application
which reproduces the problem it shouldn't be hard for someone here to
help you fix it.
> Can asynchronous comunication garantee that the confirmation socket will
> arrive in the correct order (one after each sended socket)?
No more so than using threads, unless your problem is caused by the
threads themselves (as I suggested above) in which case it might be
easier to just fix the problem.
-Peter
More information about the Python-list
mailing list