Threading tutorial wanted.

Gerhard Häring gerhard at bigfoot.de
Thu Jun 13 10:08:34 EDT 2002


Alex Polite wrote in comp.lang.python:
> Okidoki. My little webspider has reached the point where further
> optimization won't pay off. But my DSL pipe is not saturated by
> far. So I guess it's time for me to start looking into threads.

You could take a look at the linkchecker source code. It uses threads
for web spidering.

> I few minutes of googling on python + threads doesn't yield a
> lot. Maybe someone here can point me to a good primer. Maybe someone
> here can write a good primer.

Aahz has written a tutorial that you were already referred to. It's
useful info, but nowhere near complete. Maybe you can write a tutorial
yourself once you've mastered threading? ;-) Maybe there's a Python
book that covers multithreading in depth? I'd be interested which one
this is if so.

> When is threading good, when is it bad?

I've recently experimented myself with multitheading in Python. It can
open a whole new can of worms for your app, that's for sure.

> How much memory do they consume?

Depends on the threading implementation - just try it out. I don't
think it will be a problem unless start _lots_ (100+) of threads.

> Is it safe to let multiple threads use the same db connection?

Look into the DB-API specification for the threadsafety attribute of
your respective DB module. The ones I use have threadsafety=1, which
means you can't safely share the connection among threads, but it's ok
if every thread opens its own connection.

Alternatively, you could make the connection threadsafe by wrapping it
with a proxy class like the one I yesterday proposed here in:

Message-ID: <20020611205423.GA28113 at lilith.my-fqdn.de>
RfD: Automagically making a class instance 'synchronized' (draft impl.)

Or you could use a database connection pool. Webware includes one that
you can borrow.

Gerhard
-- 
mail:   gerhard <at> bigfoot <dot> de       registered Linux user #64239
web:    http://www.cs.fhm.edu/~ifw00065/    OpenPGP public key id AD24C930
public key fingerprint: 3FCC 8700 3012 0A9E B0C9  3667 814B 9CAA AD24 C930
reduce(lambda x,y:x+y,map(lambda x:chr(ord(x)^42),tuple('zS^BED\nX_FOY\x0b')))



More information about the Python-list mailing list