473,772 Members | 3,148 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Thread Question

Hi,

I have some basic doubts about thread.

I have a list which has items in it which need to be downloaded from
the internet.
Let's say list is:

list_items[] which has 100 items in it.

I have a function download_from_w eb() which does the work of
downloading the items from the web. It does error handling in case it
gets errors like 404 et cetera.

Currently, this is how I'm using it.

for item in list_items:
download_from_w eb(item)

This way, one items is downloaded at a time.

I'm planning to implement threads in my application so that multiple
items can be downloaded concurrently. I want the thread option to be
user-defined.

Looking at the documentation of threads (Core Python Programming), I've
noticed that all threads are executed a once. Depending upon what they
are doing, some finish early and some later.

But I want to implement something like:

for item in list_items:
for num in thread_args:
thread[num].start()
thread[num].start()

Is this the correct way of threading applications ?
This is the first time I'd be doing threading. So was looking for
comments and suggestions.

Thanks,
Ritesh

Jul 27 '06 #1
27 1739
Ritesh Raj Sarraf wrote:
I'm planning to implement threads in my application so that multiple
items can be downloaded concurrently. I want the thread option to be
user-defined.

Looking at the documentation of threads (Core Python Programming), I've
noticed that all threads are executed a once. Depending upon what they
are doing, some finish early and some later.

But I want to implement something like:

for item in list_items:
for num in thread_args:
thread[num].start()
thread[num].start()

Is this the correct way of threading applications ?
This is the first time I'd be doing threading. So was looking for
comments and suggestions.
What you want is to use a pool of threads so that you can configure how
many requests are issued at a time (you don't want to try to issue 100
requests all in parallel). You can communicate with the threads through a
Queue.

So if the code for a thread looks like:

def run(request, response):
while 1:
item = request.get()
if item is None:
break
response.put(do wnload_from_web (item))

# your main loop can be something like:

requestQueue = Queue()
responseQueue = Queue()
thread_pool = [
Thread(target=r un, args=(requestQu eue, responseQueue)
for i in range(numthread s)]
for t in thread_pool: t.start()

for item in list_items:
requestQueue.pu t(item)

for i in range(len(list_ items)):
response = responseQueue.g et()
handle_response (response)

# and then to shut down the threads when you've finished:
for t in thread_pool:
requestQueue.pu t(None)
for t in thread_pool:
t.join()

Jul 27 '06 #2
Duncan,

I couldn't make out much from the code.
Instead this is what I did.

threads = []
nloops = range(len(lRawD ata))
for i in nloops:
(sUrl, sFile, download_size, checksum) =
stripper(lRawDa ta[i])
t = threading.Threa d(target=downlo ad_from_web, args=(sUrl,
sFile, sSourceDir, None))
# = pypt_thread(dow nload_from_web, i,
stripper(lRawDa ta[i]))
threads.append( t)

i = 0
join_i = 0
while i < nloops:
counter = 0
while counter < 3:
threads[i].start()
counter += 1
i += 1
counter = 0
join_i = i - 3
while counter < 3:
threads[join_i].join()
counter += 1
join_i += 1

Is this correct ? Comments!!

Ritesh
Duncan Booth wrote:
Ritesh Raj Sarraf wrote:
I'm planning to implement threads in my application so that multiple
items can be downloaded concurrently. I want the thread option to be
user-defined.

Looking at the documentation of threads (Core Python Programming), I've
noticed that all threads are executed a once. Depending upon what they
are doing, some finish early and some later.

But I want to implement something like:

for item in list_items:
for num in thread_args:
thread[num].start()
thread[num].start()

Is this the correct way of threading applications ?
This is the first time I'd be doing threading. So was looking for
comments and suggestions.

What you want is to use a pool of threads so that you can configure how
many requests are issued at a time (you don't want to try to issue 100
requests all in parallel). You can communicate with the threads through a
Queue.

So if the code for a thread looks like:

def run(request, response):
while 1:
item = request.get()
if item is None:
break
response.put(do wnload_from_web (item))

# your main loop can be something like:

requestQueue = Queue()
responseQueue = Queue()
thread_pool = [
Thread(target=r un, args=(requestQu eue, responseQueue)
for i in range(numthread s)]
for t in thread_pool: t.start()

for item in list_items:
requestQueue.pu t(item)

for i in range(len(list_ items)):
response = responseQueue.g et()
handle_response (response)

# and then to shut down the threads when you've finished:
for t in thread_pool:
requestQueue.pu t(None)
for t in thread_pool:
t.join()
Jul 27 '06 #3
Ritesh Raj Sarraf wrote:
Duncan,

I couldn't make out much from the code.
Please, try again to understand Duncan's code. It's much better than
what you did.
Instead this is what I did.

threads = []
nloops = range(len(lRawD ata))
for i in nloops:
(sUrl, sFile, download_size, checksum) =
stripper(lRawDa ta[i])
t = threading.Threa d(target=downlo ad_from_web, args=(sUrl,
sFile, sSourceDir, None))
# = pypt_thread(dow nload_from_web, i,
stripper(lRawDa ta[i]))
threads.append( t)

i = 0
join_i = 0
while i < nloops:
counter = 0
while counter < 3:
threads[i].start()
counter += 1
i += 1
counter = 0
join_i = i - 3
while counter < 3:
threads[join_i].join()
counter += 1
join_i += 1

Is this correct ? Comments!!
This is just painful. It's not exactly "incorrect" , but I think (I
*think*, it's hard to tell even after reading it 3 times) that it's
going to only download three requests at a time and if one (or more) of
the requests takes a long time it will hold up all the others. Also,
you're creating one thread per item in lRawData even though you only
need/use three at a time.

if you set numthreads = 3 in Duncan's code, you would only be
downloading 3 things at a time, but you'd only be using three threads
and long running requests would not affect the others.

If you need help understanding it please ask questions. I, for one,
would be happy to comment it for you to explain how it works. It's so
nice and elegant that I've already cut-and-pasted it into my own
"notebook" of cool useful python "patterns" to use in the future.

Reread it slowly, think about what it's doing, if questions arise write
them down and ask them. Duncan's code is beautiful. It's well worth
your time to understand it.

Peace,
~Simon
>
Ritesh
Duncan Booth wrote:
Ritesh Raj Sarraf wrote:
I'm planning to implement threads in my application so that multiple
items can be downloaded concurrently. I want the thread option to be
user-defined.
>
Looking at the documentation of threads (Core Python Programming), I've
noticed that all threads are executed a once. Depending upon what they
are doing, some finish early and some later.
>
But I want to implement something like:
>
for item in list_items:
for num in thread_args:
thread[num].start()
thread[num].start()
>
Is this the correct way of threading applications ?
This is the first time I'd be doing threading. So was looking for
comments and suggestions.
>
What you want is to use a pool of threads so that you can configure how
many requests are issued at a time (you don't want to try to issue 100
requests all in parallel). You can communicate with the threads through a
Queue.

So if the code for a thread looks like:

def run(request, response):
while 1:
item = request.get()
if item is None:
break
response.put(do wnload_from_web (item))

# your main loop can be something like:

requestQueue = Queue()
responseQueue = Queue()
thread_pool = [
Thread(target=r un, args=(requestQu eue, responseQueue)
for i in range(numthread s)]
for t in thread_pool: t.start()

for item in list_items:
requestQueue.pu t(item)

for i in range(len(list_ items)):
response = responseQueue.g et()
handle_response (response)

# and then to shut down the threads when you've finished:
for t in thread_pool:
requestQueue.pu t(None)
for t in thread_pool:
t.join()
Jul 27 '06 #4
Ritesh Raj Sarraf wrote:
Duncan,

I couldn't make out much from the code.
Instead this is what I did.

threads = []
nloops = range(len(lRawD ata))
for i in nloops:
(sUrl, sFile, download_size, checksum) =
stripper(lRawDa ta[i])
t = threading.Threa d(target=downlo ad_from_web, args=(sUrl,
sFile, sSourceDir, None))
# = pypt_thread(dow nload_from_web, i,
stripper(lRawDa ta[i]))
threads.append( t)

i = 0
join_i = 0
while i < nloops:
counter = 0
while counter < 3:
threads[i].start()
counter += 1
i += 1
counter = 0
join_i = i - 3
while counter < 3:
threads[join_i].join()
counter += 1
join_i += 1

Is this correct ? Comments!!
This is bad because you start one thread for each URL then let them run in
batches of 3. So you get all the overhead of creating lots of threads and
very little of the benefit. Much better just to create 3 threads and let
each one handle as many of the requests as it can.
Jul 27 '06 #5
Simon Forman wrote:
If you need help understanding it please ask questions. I, for one,
would be happy to comment it for you to explain how it works. It's so
nice and elegant that I've already cut-and-pasted it into my own
"notebook" of cool useful python "patterns" to use in the future.

Reread it slowly, think about what it's doing, if questions arise write
them down and ask them. Duncan's code is beautiful. It's well worth
your time to understand it.
If you convert what I wrote into an example which actually runs then do
repost it. It might also be easier to understand since I'm sure I'll have
made some mistakes or omitted some critical bits (such as the import
lines).
Jul 27 '06 #6
Ritesh Raj Sarraf wrote:
[snip]
for item in list_items:
download_from_w eb(item)

This way, one items is downloaded at a time.

I'm planning to implement threads in my application so that multiple
items can be downloaded concurrently. I want the thread option to be
user-defined.
[snip]

See my post about the iterthreader module I wrote...
http://groups.google.com/group/comp....29fae28cf44c1/

for url, result in Threader(downlo ad_from_web, list_items):
print url, result
#...

--
- Justin

Jul 27 '06 #7
Duncan Booth wrote:
Simon Forman wrote:
If you need help understanding it please ask questions. I, for one,
would be happy to comment it for you to explain how it works. It's so
nice and elegant that I've already cut-and-pasted it into my own
"notebook" of cool useful python "patterns" to use in the future.

Reread it slowly, think about what it's doing, if questions arise write
them down and ask them. Duncan's code is beautiful. It's well worth
your time to understand it.

If you convert what I wrote into an example which actually runs then do
repost it. It might also be easier to understand since I'm sure I'll have
made some mistakes or omitted some critical bits (such as the import
lines).
A pleasure.

There was one missing close-parenthesis in the Thread() call, but other
than that (and the implied imports and variables) everything was good.

I added some dummy code to simulate long-running requests, and I made
the run() function return its Thread's name, but other than that it's
basically the code you posted. It's neat to increase NUMTHREADS and
watch the script run faster. :)

When the time comes to use this in something I'll probably wrap it up
in a class, and add some error handling to the run() function (or
method). But in the meantime it's quite elegant and useful as is.
Definitely not not Scottish ;-)

Thanks Duncan.

Peace,
~Simon

from Queue import Queue
from threading import Thread, currentThread

# Imports for the dummy testing func.
from time import sleep
from random import random

NUMTHREADS = 3
def dummy_func(secs ):
'''
Sleep for secs seconds then return secs.
(Dummy function to simulate requests.)
'''
sleep(secs)
return secs

# Some fake requests to pass to dummy_func().
dummy_requests = [
5 * random() for notused in xrange(100)
]

# Dummy handle_response () for demo purposes.
def handle_response (resp): print resp
def run(request, response, func=dummy_func ):
'''
Get items from the request Queue, process them
with func(), put the results along with the
Thread's name into the response Queue.

Stop running once an item is None.
'''
name = currentThread() .getName()
while 1:
item = request.get()
if item is None:
break
response.put((n ame, func(item)))
# Create two Queues for the requests and responses
requestQueue = Queue()
responseQueue = Queue()
# Pool of NUMTHREADS Threads that run run().
thread_pool = [
Thread(
target=run,
args=(requestQu eue, responseQueue),
name="Thread %i" % i
)
for i in range(NUMTHREAD S)
]
# Start the threads.
for t in thread_pool: t.start()
# Queue up the requests.
for item in dummy_requests: requestQueue.pu t(item)
# Shut down the threads after all requests end.
# (Put one None "sentinel" for each thread.)
for t in thread_pool: requestQueue.pu t(None)
# Get and handle each response.
for notused in xrange(len(dumm y_requests)):
response = responseQueue.g et()
handle_response (response)
# Don't end the program prematurely.
#
# (Note that because Queue.get() is blocking by
# default this isn't strictly necessary. But if
# you were, say, handling responses in another
# thread, you'd want something like this in your
# main thread.)
for t in thread_pool: t.join()

###
Example output from a run of the above script:

('Thread 0', 0.1091546275106 8916)
('Thread 0', 0.2942818913462 9135)
('Thread 1', 1.6234285192453 246)
('Thread 0', 3.1957991561450 96)
('Thread 1', 2.7641123440885 367)
('Thread 2', 4.7810243032096 862)
('Thread 2', 1.1752965020601 662)
('Thread 1', 2.0727863018148 924)
('Thread 0', 4.8127195859913 252)
('Thread 1', 2.4780495377626 242)
..
..
..
etc...

Jul 27 '06 #8
Duncan Booth on Thursday 27 Jul 2006 17:17 wrote:
What you want is to use a pool of threads so that you can configure how
many requests are issued at a time (you don't want to try to issue 100
requests all in parallel). You can communicate with the threads through a
Queue.
Thanks to both Duncan and Simon for your motivating replies. I had forgotten the
truth that the lists are a medium to learn and educate.
I hope you won't be very frustrated by my dummy questions.
So if the code for a thread looks like:

def run(request, response):
while 1:
item = request.get()
if item is None:
break
response.put(do wnload_from_web (item))
This is where I'm most confused.

I have a list, lRawData which has around, say, 100 items which are read from a
file.
Currently (without threads), I iterate upon each item in the list, split the
item and then manipulate and pass it to download_from_w eb() to do the real
work.

I'm mainly confused in your run().
The line response.put(do wnload_from_web (item)) is there. But if I use it, how am
I going to iterate on my list lRawData.
I'm not able to see where and how, run() or the line item = request.get() goes
into my list lRawData to get the items.

Can you please explain the functioning of the above code block.
# your main loop can be something like:

requestQueue = Queue()
responseQueue = Queue()
thread_pool = [
Thread(target=r un, args=(requestQu eue, responseQueue)
for i in range(numthread s)]
for t in thread_pool: t.start()
This part seems understandable.
>
for item in list_items:
requestQueue.pu t(item)

for i in range(len(list_ items)):
response = responseQueue.g et()
handle_response (response)
I guess these would be clear once I'm clear on the first problem.
# and then to shut down the threads when you've finished:
for t in thread_pool:
requestQueue.pu t(None)
for t in thread_pool:
t.join()
Thanks a lot for your replies.

Ritesh
--
Ritesh Raj Sarraf
RESEARCHUT - http://www.researchut.com
"Necessity is the mother of invention."
"Stealing logic from one person is plagiarism, stealing from many is research."
"The great are those who achieve the impossible, the petty are those who
cannot - rrs"

Jul 28 '06 #9
And people, Is there any documentation on Python Threads or Threads in general.
It'd be of great help to really understand.

Ritesh

Ritesh Raj Sarraf on Thursday 27 Jul 2006 16:37 wrote:
Is this the correct way of threading applications ?
This is the first time I'd be doing threading. So was looking for
comments and suggestions.
--
Ritesh Raj Sarraf
RESEARCHUT - http://www.researchut.com
"Necessity is the mother of invention."
"Stealing logic from one person is plagiarism, stealing from many is research."
"The great are those who achieve the impossible, the petty are those who
cannot - rrs"

Jul 28 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
6022
by: rnn98 | last post by:
hi, my multithread application, running under solaris box, is crashing eventually. I tried to spot and substitute functions not "thread safe", but I guess my search wasn't good enough. I have put localtime_r and asctime_r where I was using localtime and asctime. But I didn't find any correspondent _r function to "time".Also, I found that "readdir" is not thread safe. But what about "scandir"? Thanks for any help
5
2599
by: Bill Davidson | last post by:
Hello All: I've got a question about synchronization requiremements in a C# worker thread procedure that, among other things, sinks events from outside sources. I realize the worker thread will be interrupted to handle an incoming event (and the flow of execution subsequently diverted to the respective event handler). My question is at what point or points could this interruption occur. Will it only occur when the worker thread is...
1
1785
by: Bill Davidson | last post by:
(RESEND: I added a little more code to the sample for clarity) Hello All: I've got a question about synchronization requiremements in a C# worker thread procedure that, among other things, sinks events from outside sources. I realize the worker thread will be interrupted to handle an incoming event (and the flow of execution subsequently diverted to the respective event handler). My question is at what point or points could this...
2
1863
by: James Lavery | last post by:
Hi everyone, We're developing an application to capture data from several serial ports, store in a database, and (optionally) forward on using FTP. Each serial port is being processed in a thread, so that capture of data can continue if the UI is blocked with a modal dialg, for example. I would like each thread to be able to pass any FTP work to a dedicated thread, so that its data capture is not interrupted if the
22
4097
by: Morpheus | last post by:
Hi, I have been coding in Windows for many years so have a mindset to it, so forgive any stupid questions. Is it possible to create a multithread application in C++ that is portable (OS/compiler)? In my Windows applications, I use synchronization objects like, mutex, semaphores etc. Is there anything similar in the Standard ANSI C++ or would I be best off doing this myself, possibly creating a new class to handle any critical...
6
6871
by: Sergey Poberezovskiy | last post by:
I have the following code in C# that I have trouble converting to VB(2.0): private delegate void openDialog(); private void openWindowsDialog(openDialog open) { Thread thread = new Thread(new ThreadStart(open)); thread.SetApartmentState(ApartmentState.STA); thread.Start(); }
34
2817
by: Creativ | last post by:
Why does Thread class not support IDisposable? It's creating quite some problem. Namely, it can exhaust the resource and you have not control over it.
19
2313
by: Hapa | last post by:
Does only reading (never writing) of a variable need thread synchronisation? Thanks for help? PS. Anybody knows a Visual C++ news group?
9
6990
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
I've got a routine that builds a table using different queries, different SQL Tables, and adding custom fields. It takes a while to run (20 - 45 seconds) so I wrote a thread to handle the table population. Whenever I call the thread, I pass it a structure containing the table and a few other parameters. The table goes in as a reference, but the other items are passed normally.
2
2200
by: k3xji | last post by:
Hi all, This will probably be a long question/short answer, sorry, but I have wandered net about the subject and really feel cannot find just enough information.I want to ask my question by giving an example to explicitly express my understanding which may be also wrong: So, let's have a string in a module like: st = 'IAmAstring' My understanding: Python allocates a string object and binds the st to
0
9620
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10104
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9912
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.