Do I have to use threads?

r0g aioe.org at technicalbloke.com
Fri Jan 8 09:31:21 EST 2010


Marco Salden wrote:
> On Jan 6, 5:36 am, Philip Semanchuk <phi... at semanchuk.com> wrote:
>> On Jan 5, 2010, at 11:26 PM, aditya shukla wrote:
>>
>>> Hello people,
>>> I have 5 directories corresponding 5  different urls .I want to  
>>> download
>>> images from those urls and place them in the respective  
>>> directories.I have
>>> to extract the contents and download them simultaneously.I can  
>>> extract the
>>> contents and do then one by one. My questions is for doing it  
>>> simultaneously
>>> do I have to use threads?
>> No. You could spawn 5 copies of wget (or curl or a Python program that  
>> you've written). Whether or not that will perform better or be easier  
>> to code, debug and maintain depends on the other aspects of your  
>> program(s).
>>
>> bye
>> Philip
> 
> Yep, the more easier and straightforward the approach, the better:
> threads are always (programmers')-error-prone by nature.
> But my question would be: does it REALLY need to be simultaneously:
> the CPU/OS only has more overhead doing this in parallel with
> processess. Measuring sequential processing and then trying to
> optimize (e.g. for user response or whatever) would be my prefered way
> to go. Less=More.
> 
> regards,
> Marco



Threads aren't as hard a some people make out although it does depend on
the problem. If your processes are effectively independent then threads
are probably the right solution. You can turn any function into a thread
quite easily, I posted a function for this a while back...

http://groups.google.com/group/comp.lang.python/msg/3361a897db3834b4?dmode=source

Also it's often a good idea to build in a flag that switches your app
from multi threaded to single threaded as it's easier to debug the latter.

Roger.



More information about the Python-list mailing list