why am I getting a segmentation fault?

Paul McGuire ptmcg at austin.rr._bogus_.com
Fri Jan 21 17:22:03 EST 2005

"Jay donnell" <jaydonnell at gmail.com> wrote in message
news:1106330476.419418.46920 at f14g2000cwb.googlegroups.com...
> I have a short multi-threaded script that checks web images to make
> sure they are still there. I get a segmentation fault everytime I run
> it and I can't figure out why. Writing threaded scripts is new to me so
> I may be doing something wrong that should be obvious :(

Here is a code excerpt from your link (the main routine, omits the class
definition for ImageChecker, which extends threading.Thread):

db = MySQLdb.connect(host="localhost", user="xxx", passwd="xxx", db="xxx")
cursor = db.cursor()
query = "select * from item order by rand() limit 0, 100"
#query = "select * from item"
result = cursor.fetchall()

maxThreads = 5

for r in result:
    while(threading.activeCount() > maxThreads):
    #print str(r[0]) + ', ' + str(r[7])
    tmp = r[7].split('/')
    filename = tmp[-1]
    #print 'filename ' + filename

    filename = '/tmp/'+filename

    threadList = []

    #r[7] is the url of the image
    #r[0] is the id for the row
    imageChecker = ImageChecker(r[7], filename, r[0])

1. What happens after you fall out of the loop "for r in result"?  Shouldn't
you wait for the remaining threads to finish?  Perhaps you need another
busy-loop to wait for the threads to finish, something like
    while threading.activeCount() > 0: pass
2. Is this the best way to busy-wait?  What about some kind of
thread.join()?  At least throw a sleep call in there or something, or this
loop will churn and churn, diverting CPU from your threads that are actually
trying to do some real work.
3. I find it easier to work with named variables than numeric subscripts.
At the top of your for loop, try something like:
    id,height,width,numBytes,whatever,slkdjf1,slkdjf2,url = r
This way you have much more meaningful names than r[0] and r[7], which you
later have to comment to explain whats going on!
4. filename=r[7].split('/')[-1] is not terribly portable.  See if there is a
standard module for parsing filespecs (I'll bet there is).

-- Paul

More information about the Python-list mailing list