Interview Helper
Translate Page To German Tranlate Page To Spanish Translate Page To French Translate Page To Italian Translate Page To Japanese Translate Page To Korean Translate Page To Portuguese Translate Page To Chinese
[Valid RSS feed]
 
Number Times Read : 309    
 
  Go To Article :    
Categories

Air India (2)
Campus Interviews (505)
Career Helper (6)
Certification Dumps (10)
Code Samples (118)
Competitive Exams (163)
Interview Questions (4686)
Test Dev (0)
Test Director (25)
test... (0)
test_php (0)
Walkin Interviews (20)
 
Stats
Total Articles: 5572
Total Authors: 781
Total Downloads: 1183533


Newest Member
sukhvir chahal
 
   

Question Popularity: 99 or more times read
Submitted 2009-01-19 02:46:32 [Valid RSS feed]


How do I parcel out work among a bunch of worker threads
Question:How do I parcel out work among a bunch of worker threads?

Answer:Use the Queue module to create a queue containing a list of jobs. The Queue class maintains a list of objects with .put(obj) to add an item to the queue and .get() to return an item. The class will take care of the locking necessary to ensure that each job is handed out exactly once.



Here's a trivial example:



import threading, Queue, time



# The worker thread gets jobs off the queue. When the queue is empty, it

# assumes there will be no more work and exits.

# (Realistically workers will run until terminated.)

def worker ():

print 'Running worker'

time.sleep(0.1)

while True:

try:

arg = q.get(block=False)

except Queue.Empty:

print 'Worker', threading.currentThread(),

print 'queue empty'

break

else:

print 'Worker', threading.currentThread(),

print 'running with argument', arg

time.sleep(0.5)



# Create queue

q = Queue.Queue()



# Start a pool of 5 workers

for i in range(5):

t = threading.Thread(target=worker, name='worker %i' % (i+1))

t.start()



# Begin adding work to the queue

for i in range(50):

q.put(i)



# Give threads time to run

print 'Main thread sleeping'

time.sleep(5)



When run, this will produce the following output:



Running worker Running worker Running worker Running worker Running worker Main thread sleeping Worker running with argument 0 Worker running with argument 1 Worker running with argument 2 Worker running with argument 3 Worker running with argument 4 Worker running with argument 5 ...



Related Stuff
Answers
No answer submitted yet!! Be the first one to answer this question.
New Members
select
Sign up
select
learn more
Affiliate Sign in
Affiliate Sign In
 
Nav Menu
Home
Login
Submit Articles
Submission Guidelines
Top Articles
Link Directory
About Us
Contact Us
Privacy Policy
RSS Feeds

Actions
Print This Article
Add To Favorites

 
Sponsors

 

All Rights Reserved Interview Helper

Proudly associated with Job Interview Helper | Interviewhelper Articles