473,378 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

asynchronous comunication, wxPython and threads.

Hi,

I have the following problem.

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.
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 have been told not to do comunication in a new thread; instead, I should
use asyncronus comunication.

My question:

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)?
Can asynchronous comunication garantee that the confirmation socket will
arrive in the correct order (one after each sended socket)?

Thanks for your help

Zunbeltz Izaola
Jul 19 '05 #1
6 2903
On Tuesday 21 June 2005 14:22, Zunbeltz Izaola wrote:
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 guess you are accessing the socket from both your GUI thread and
communications thread. Dont do that. An action in the GUI thread should
signal the communictions thread, then the communictions thread talks to the
socket.
I have been told not to do comunication in a new thread; instead, I should
use asyncronus comunication.
Using non-blocking sockets in the GUI thread may cause the opposite problem to
the one that led you to use threads in the first place: a blocking operation
in the GUI may freeze the communications. Maybe that isnt a problem for you.
If it is, I suggest sticking to two threads.
What module should i use, asyncore, asynchat, twisted,(anohter)


If you are talking to only one device, then using blocking sockets is a good
approach. However Ive never written an application like this that didnt need
to support a second (or third) machine sooner or later, and three
communictions threads is starting to get ugly. A framework like Twisted will
let you handle many machines in the one thread, but it still makes sense to
keep a second one for the GUI.
--
Toby Dickenson
Jul 19 '05 #2
On Tue, 21 Jun 2005 15:30:41 +0100, Toby Dickenson wrote:
On Tuesday 21 June 2005 14:22, Zunbeltz Izaola wrote:
I guess you are accessing the socket from both your GUI thread and
communications thread. Dont do that. An action in the GUI thread should
signal the communictions thread, then the communictions thread talks to the
socket.

I see ..., Could be the problem that the socket is created in the GUI
thread? the function that end the theraded function (abort())
set want_abort = True
This make the Measurement() function to return. The Measurement()
funtion is called by startmeasurement() which is the threaded funciton.
After aborting i execute a function that FinalizeMeasuremnt() that
does comunication to some adjustament in the machine. Maybe i have to move
this portion to the threaded funtion.


Using non-blocking sockets in the GUI thread may cause the opposite problem to
the one that led you to use threads in the first place: a blocking operation
in the GUI may freeze the communications. Maybe that isnt a problem for you.
If it is, I suggest sticking to two threads.

Yes, i think i have to stick to two threads.

If you are talking to only one device, then using blocking sockets is a good
approach. However Ive never written an application like this that didnt need
to support a second (or third) machine sooner or later, and three
communictions threads is starting to get ugly. A framework like Twisted will
let you handle many machines in the one thread, but it still makes sense to
keep a second one for the GUI.


I didn't get this. What do you do when you have more tham one device; use thread or use
Twisted? Did you use it whit wxPython. I think thereis some problems of compatibility.
Thank you very much.

Zunbeltz

Jul 19 '05 #3
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
Jul 19 '05 #4
On Tue, 21 Jun 2005 11:07:35 -0400, Peter Hansen wrote:

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?

Sorry for the lack of clarity. I opened the socket once (i don't know if
itit is important to open inside or outside the comunication thread). And
them send "packages" and wait for data.


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.

Thanks for the vocabulary correction.
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 think this is the case (see the post of Toby). I didn't try it out but I
think the problem is that i *do* comunication in both threads.

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.

Yes, i have read that there is problems yet.

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.

Thanks. First i would check if the problem is what Toby says.

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


Thank you very much

Zunbeltz

Jul 19 '05 #5
Zunbeltz Izaola wrote:
I opened the socket once (i don't know if
itit is important to open inside or outside the comunication thread).
I don't believe it is important where it is opened, provided you don't
try to do things with it from two threads at a time (without adequate
protection).
I didn't try it out but I
think the problem is that i *do* comunication in both threads.


Almost certainly it is. It would be simplest to set up a worker thread
once, when the GUI thread begins, and simply send requests to it via a
Queue. It can create the socket, connect to the server, communicate,
close it down, and go back to waiting all in one place (so to speak...
of course this would be several methods all called from a top-level loop
in that thread). No chance of mis-steps.

-Peter
Jul 19 '05 #6
On Wed, 22 Jun 2005 09:28:31 -0400, Peter Hansen wrote:

Almost certainly it is. It would be simplest to set up a worker thread
once, when the GUI thread begins, and simply send requests to it via a
Queue. It can create the socket, connect to the server, communicate,
close it down, and go back to waiting all in one place (so to speak...
of course this would be several methods all called from a top-level loop
in that thread). No chance of mis-steps.


I corrected the problem. A bit of the comunication was done in the
GUI thread.

Thanks again for your help.

Zunbeltz
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Zunbeltz Izaola | last post by:
Hi I'm writting a program to get data from a machine. I'm using wxPython to build the GUI. I've a mehtod in a class that get the data, something like this: data = def Measure(self): do...
5
by: Marty McDonald | last post by:
I create and start several threads, each thread executes the same method - within the method, a web service is invoked. I find that the more threads I use, the longer it takes for all of the...
4
by: Matthew Groch | last post by:
Hi all, I've got a server that handles a relatively high number of concurrent transactions (on the magnitude of 1000's per second). Client applications establish socket connections with the...
4
by: taskswap | last post by:
I have a legacy application written in C that I'm trying to convert to C#. It processes a very large amount of data from many clients (actually, upstream servers - this is a mux) simultaneously. ...
4
by: Macca | last post by:
I am writing an application that uses asynchronous sockets to get data over ethernet from embedded devices, up to 30 concurrent devices.(These devices are written in C). My application...
3
by: senfo | last post by:
I recently read an MSDN article by Jeff Prosise titled, Scalable Apps with Asynchronous Programming in ASP.NET (http://msdn.microsoft.com/msdnmag/issues/07/03/WickedCode/). In the article, Jeff...
1
by: Morgan Cheng | last post by:
I have one webservice that involves time-costing computation. For each request, it consumes about 2 seconds computation. Since ASP.NET has 25 threads per CPU to handle requests, this delay turns to...
1
by: Benjamin | last post by:
Hello! I am writing a search engine with wxPython as the GUI. As the search thread returns items, it adds them to a Queue which is picked up by the main GUI thread calling itself recursively with...
12
by: bullockbefriending bard | last post by:
I am a complete ignoramus and newbie when it comes to designing and coding networked clients (or servers for that matter). I have a copy of Goerzen (Foundations of Python Network Programming) and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.