Newbie prob: How to write a file with 3 threads?

Bjoern Schliessmann usenet-mail-0306.20.chr0n0ss at spamgourmet.com
Wed May 9 10:49:30 EDT 2007


est wrote:

> I'd like to say VERY VERY VERY thank you for your detailed
> information, that's a lot encourage for a beginner. In fact I am
> writing a multi thread download ultility, and I found that very
> hard for me. 

You don't need any system threads for multiple download threads.
Since network connections are buffered and much slower than CPU
cycles, you can also use event based mechanisms. (I've written 30+
connection servers like this already.)

The general pseudocode strategy is 

while True:
    waitForEvents()
    event = getEvent()
    processEvent(event)
    # in here, you inspect the event, 
    # e. g. from which connection it is,
    # and get or write data

The only drawback to event based programming is that processor
cycle "rationing" is decided in your code and not by the OS, which
can make difficulties with really long calculations. But if these
problems arise you usually spawn a seperate worker thread :)

> Can you recommand any sample code where I can start 
> with? 

If you choose event based programming, definitely have a look at
Twisted. It lets you create multiple connection clients and servers
in few code lines without synchronisation hassle.

http://twistedmatrix.com/projects/core/documentation/howto/
http://twistedmatrix.com/projects/core/documentation/howto/clients.html

(BTW, don't get scared by "Deferred"s, I never explicitly used them
in my code ... though they often work in the background)

Regards,


Björn

-- 
BOFH excuse #376:

Budget cuts forced us to sell all the power cords for the servers.




More information about the Python-list mailing list