471,073 Members | 1,359 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,073 software developers and data experts.

is there any bug in this multi-thread script?

I wrote a script to do work with multi-thread in a queue reference
others code.
Is there any bug in this script?
another question is : when we call start to run one thread extends from
threading, it will call a join() by itself? I want to monitor the
qsize() to ensure all work are finished. but when I commented the
#############code start
while q.qsize()>0:
time.sleep(0.1)
#############code end
it works well too.
any response is welcome.
Thanks a lot.

#############code start

#!/usr/bin/env python
import Queue
import threading
import time
import random

q=Queue.Queue(0)
NUM_WORKERS = 3

class MyThread(threading.Thread):
"""A worker thread."""
def __init__(self, input, worktype):
self._jobq = input
self._work_type = worktype
threading.Thread.__init__(self)
def run(self):
"""
Get a job and process it. Stop when there's no more jobs
"""
while True:
if self._jobq.qsize()>0:
job = self._jobq.get()
worktype=self._work_type
self._process_job(job,worktype)
else:
break
def _process_job(self, job,worktype):
"""
Do useful work here.
worktype: let this thread do different work
1,do list
2,do item
3,,,
"""
doJob(job)

def doJob(job):
"""
do work function 1
"""
time.sleep(random.random()*3)
print "doing ",job
if __name__=='__main__':

print "begin..."
#put some work to q
for i in range(NUM_WORKERS*2):
q.put(i)
#print total job q's size
print "job q'size",q.qsize()
#start threads to work
for x in range(NUM_WORKERS):
MyThread(q,x).start()
#if q is not empty, wait
#while q.qsize()>0:
# time.sleep(0.1)

Apr 5 '06 #1
1 922
up...

Apr 6 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Neil Zanella | last post: by
37 posts views Thread by ajikoe | last post: by
reply views Thread by Ganbold | last post: by
23 posts views Thread by Kaz Kylheku | last post: by
17 posts views Thread by =?Utf-8?B?R2Vvcmdl?= | last post: by
3 posts views Thread by Fred | last post: by

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.