By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,089 Members | 2,418 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,089 IT Pros & Developers. It's quick & easy.

Advice: socket handling and threads

P: n/a
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


Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
> 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
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.