Advice: socket handling and threads

D

Dan Williams

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
 
D

Diez B. Roggisch

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top