Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Advice: socket handling and threads

Reply
Thread Tools

Advice: socket handling and threads

 
 
Dan Williams
Guest
Posts: n/a
 
      02-04-2004
Hi people

Part of the reason I have started using Python (experience: 3 days) is that
I recognise the potential benefits in using it compared to writing C/C++
code. In particular, there are a couple of modules in an application that I
am writing that I have calculated will be up to ten times quicker to develop
(although it is hard to say at this stage), which is obviously quite
attractive.

The "problem" I have is relating to the existing C++ code I have. I've
started porting it to Python, so that the whole project will be in the same
language. The main aspects of my app that I am wondering about are socket
handling and multi-threading.

As far as I understand, Python can do everything that C/C++ can do with
sockets, plus there are the thread and threading modules which I believe are
equivalent to the pthreads module I have been using so far. My question is
this:

Is it sensible to port these sections of code to Python? Do I stand to gain
or lose by doing this... I doubt the performance difference will be
noticeable, and the only areas of the project that I would maybe consider
extending in C++ are to do with parsing (and to be honest I think it'll be
fine left in Python).

Finally there is the cherry on the cake in the form of cross-platform
compatibility. My C++ code only works on *nix (or cygwin) but as far as I
understand it, my threads and sockets should work fine on Windows and Mac
too, if done in Python...? (This is a CLI app not GUI by the way.)

Thoughts and general musings will be greatly appreciated (as will any
warnings of pitfalls I may encounter!)

Dan




 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      02-04-2004
> Is it sensible to port these sections of code to Python? Do I stand to
> gain or lose by doing this... I doubt the performance difference will be
> noticeable, and the only areas of the project that I would maybe consider
> extending in C++ are to do with parsing (and to be honest I think it'll be
> fine left in Python).
>
> Finally there is the cherry on the cake in the form of cross-platform
> compatibility. My C++ code only works on *nix (or cygwin) but as far as I
> understand it, my threads and sockets should work fine on Windows and Mac
> too, if done in Python...? (This is a CLI app not GUI by the way.)
>
> Thoughts and general musings will be greatly appreciated (as will any
> warnings of pitfalls I may encounter!)


You should look into python twisted, a framework for networking. It has
plenty of features. You can easily write portable networking code that
makes use of select for dispatching, so you don't need to be multithreaded.
It also supports reactors that are multithreaded, but in my expirience
thats not necessary, as multithreading often is an excuse for easier
handling of blocking io - which you avoid by the underlying select-based
infrastructure.

I had a mult-threaded client-server-app that also involved the serial line -
porting it to twisted was easy and straighforward, and now I'm rid of all
threads - which leads to way lower system load while the performance is the
same.

Regards,

Diez
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 1 02-03-2009 06:20 AM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 0 02-01-2009 12:45 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 0 02-01-2009 07:37 AM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48,'Address already in use') Jean-Paul Calderone Python 0 01-27-2009 01:41 PM



Advertisments