Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Question about thread

Reply
Thread Tools

Re: Question about thread

 
 
Jp Calderone
Guest
Posts: n/a
 
      11-19-2004
On Fri, 19 Nov 2004 22:50:17 +0800, Valkyrie <> wrote:
>To be more precise, what I want to do is to have a threaded program to handle
> some jobs concurrently (while those jobs are fetching information from the
> Internet, so threading could speed up the process if the network is slow)
>
> start
> | (blahblahblah...)
> v
> +-----+-----+-----+-----+
> | | | | |
> --+-- --+-- --+-- --+-- --+--
> | | | | | | | | | |
> | A | | B | | C | | D | | E |
> | | | | | | | | | |
> --+-- --+-- --+-- --+-- --+--
> | | | | |
> +-----+-----+-----+-----+
> | (blahblahblah...)
> v
> finish!


If your goal is efficient network concurrency, threads are a second-rate solution. Asynchronous IO is the winner: http://www.twistedmatrix.com/

Here's an example (untested as usual):

from twisted.web.client import downloadPage
from twisted.internet import defer, reactor

# Map URLs to files to which to save them
URLs = {'www.google.com', 'google',
# ...
'wigu.com', 'wigu',
}

# Initiate the download and save of each page
downloads = []
for url in URLs:
downloads.append(downloadPage(url, open(URLs[url], 'w')))

# Wait for all of the downloads to finish, then stop
defer.DeferredList(downloads).addCallback(lambda r: reactor.stop())

# Start the reactor
reactor.run()

Jp
 
Reply With Quote
 
 
 
 
Valkyrie
Guest
Posts: n/a
 
      11-19-2004
Thanks for your suggestion But I would like to have a more standard solution,
I still don't know how to do so im mind...


Jp Calderone wrote:
> On Fri, 19 Nov 2004 22:50:17 +0800, Valkyrie <> wrote:
>
>>To be more precise, what I want to do is to have a threaded program to handle
>>some jobs concurrently (while those jobs are fetching information from the
>>Internet, so threading could speed up the process if the network is slow)
>>
>> start
>> | (blahblahblah...)
>> v
>> +-----+-----+-----+-----+
>> | | | | |
>>--+-- --+-- --+-- --+-- --+--
>>| | | | | | | | | |
>>| A | | B | | C | | D | | E |
>>| | | | | | | | | |
>>--+-- --+-- --+-- --+-- --+--
>> | | | | |
>> +-----+-----+-----+-----+
>> | (blahblahblah...)
>> v
>> finish!

>
>
> If your goal is efficient network concurrency, threads are a second-rate solution. Asynchronous IO is the winner: http://www.twistedmatrix.com/
>
> Here's an example (untested as usual):
>
> from twisted.web.client import downloadPage
> from twisted.internet import defer, reactor
>
> # Map URLs to files to which to save them
> URLs = {'www.google.com', 'google',
> # ...
> 'wigu.com', 'wigu',
> }
>
> # Initiate the download and save of each page
> downloads = []
> for url in URLs:
> downloads.append(downloadPage(url, open(URLs[url], 'w')))
>
> # Wait for all of the downloads to finish, then stop
> defer.DeferredList(downloads).addCallback(lambda r: reactor.stop())
>
> # Start the reactor
> reactor.run()
>
> Jp

 
Reply With Quote
 
 
 
 
Jean Brouwers
Guest
Posts: n/a
 
      11-19-2004

Look at the example in the asyncore module in the standard Python
library.

<http://www.python.org/doc/current/lib/module-asyncore.html>


/Jean Brouwers
ProphICy Semiconductor, Inc.


In article <1100878186.26229@eng-ser6>, Valkyrie <>
wrote:

> Thanks for your suggestion But I would like to have a more standard
> solution,
> I still don't know how to do so im mind...
>
>
> Jp Calderone wrote:
> > On Fri, 19 Nov 2004 22:50:17 +0800, Valkyrie <> wrote:
> >
> >>To be more precise, what I want to do is to have a threaded program to
> >>handle
> >>some jobs concurrently (while those jobs are fetching information from the
> >>Internet, so threading could speed up the process if the network is slow)
> >>
> >> start
> >> | (blahblahblah...)
> >> v
> >> +-----+-----+-----+-----+
> >> | | | | |
> >>--+-- --+-- --+-- --+-- --+--
> >>| | | | | | | | | |
> >>| A | | B | | C | | D | | E |
> >>| | | | | | | | | |
> >>--+-- --+-- --+-- --+-- --+--
> >> | | | | |
> >> +-----+-----+-----+-----+
> >> | (blahblahblah...)
> >> v
> >> finish!

> >
> >
> > If your goal is efficient network concurrency, threads are a second-rate
> > solution. Asynchronous IO is the winner: http://www.twistedmatrix.com/
> >
> > Here's an example (untested as usual):
> >
> > from twisted.web.client import downloadPage
> > from twisted.internet import defer, reactor
> >
> > # Map URLs to files to which to save them
> > URLs = {'www.google.com', 'google',
> > # ...
> > 'wigu.com', 'wigu',
> > }
> >
> > # Initiate the download and save of each page
> > downloads = []
> > for url in URLs:
> > downloads.append(downloadPage(url, open(URLs[url], 'w')))
> >
> > # Wait for all of the downloads to finish, then stop
> > defer.DeferredList(downloads).addCallback(lambda r: reactor.stop())
> >
> > # Start the reactor
> > reactor.run()
> >
> > Jp

 
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
Terminating a thread from the main thread Charles A. Lackman ASP .Net 3 12-09-2004 02:12 PM
Thread was being aborted in win2003 server. Back ground thread reading MS access database, no redirects or transfers. Johanna ASP .Net 0 10-13-2004 01:32 PM
"Thread was being aborted" error from WebApp using Thread.Sleep. Stephen Miller ASP .Net 3 07-01-2004 11:50 PM
perl 5.8.2/3 - thread started by a thread pawo Perl 0 02-16-2004 01:18 PM
PyNew_Interpreter(): "Fatal Python error, invalid thread state for this thread" question vincent wehren Python 0 12-11-2003 08:09 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57