Creating objects in thread created with subclass

Nick Arnett narnett at mccmedia.com
Tue Apr 9 21:18:36 EDT 2002


> -----Original Message-----
> From: python-list-admin at python.org
> [mailto:python-list-admin at python.org]On Behalf Of Jeff Shannon

...

> No, that's not correct.  If you create an object in the Run
> method of your threading.Thread subclass, then you will create a
> new instance of that object for *each* instance of your subclass,
> so each thread will have a unique object created for it.  Since
> you're using a thread pool, then you'll be re-using that object
> each time that you re-use that thread, but ThreadA will have a
> different instance of it than ThreadB.

I have to say that I'm relieved to hear that... and equally relieved to find
that now that I fixed a couple of things elsewhere in the spider, it works
fine just as you say.  (And I thought it worked that way, but the other
errors were confusing me.)

Thanks!

> > So, I could create objects with 'eval' or 'exec'
> > to generate unique names for each thread -- but is that the
> right way to do
> > it?
>
> No, that is definitely *not* the right way to do it -- as I keep
> saying, exec/eval() are very rarely the right way to do it,
> whatever "it" may be.  ;)

Ah.  Well.  Then as long as we're on the subject... I couldn't figure out a
way to build an insert command for MySQLdb without it.  Given a list of
tuples to insert, I need to give the database handle a statement that looks
like this:

self.dbh.execute("INSERT INTO Foo (fieldnames) VALUES (%s,[%s])",(data[1:]))

The first tuple in the list of tuples is the fieldnames, which can vary.
Thus, the number of "%s" occurrences in the statement has to be calculated.
Is there a way to create this insertion statement without using exec?

Nick






More information about the Python-list mailing list