How to pass a list of objects to a thread function
<p>I have to pass a list of objects to a function that executes in a thread in python. I need to be able to call functions of those objects, such as <code>animal.bite()</code>. I created a generic test class:</p>

<pre><code>class test_class:    
    def f(self):
        print 'hello'
</code></pre>

<p>and created a list of these objects:</p>

<pre><code>test_object = test_class()
new_object = test_class()
strlist = [test_object, new_object]
</code></pre>

<p>and have a function that creates a thread if one hasn't already been created:</p>

<pre><code>def threadScheduler(*stringlist):
    global lock #variable defined elsewhere, makeshift resource lock
    if not lock:
        print "creating thread"
        lock = True
        thread = threading.Thread(name='heartbeat', target=threadWorkLoad, args=(stringlist,))
        thread.start()
</code></pre>

<p>This is the function <code>threadWorkLoad</code>:</p>

<pre><code>def threadWorkLoad(*stringlist):
    global lock
    for item in stringlist:
        print 'in thread', item.f()
    time.sleep(2)
    lock = False
    return
</code></pre>

<p>and this is the main loop:</p>

<pre><code>for x in range(0,10):
    print 'in main thread', strlist[0].f()
    threadScheduler(strlist)
    time.sleep(1)
</code></pre>

<p>What I would like to do is to be able to call the function <code>f()</code> on the objects in the list in <code>threadWorkLoad</code>, but currently I get the error <code>AttributeError: 'tuple' object has no attribute 'f'</code></p>

<p>If I replace those objects with strings, the code works the way I want it to. But I need to be able to do the same thing with objects. How do I do this in python?</p>

<p>EDIT:</p>

<p>Some other things I tried -</p>

<p>I changed the creation of the thread in <code>threadScheduler</code> to <code>thread = threading.Thread(name='heartbeat', target=threadWorkLoad, args=[stringlist])</code> with no luck. </p>

<p>I also tried to put in the following statement as the first line of <code>threadScheduler</code>:</p>

<p><code>print 'in scheduler', stringlist[0].f()</code></p>

<p>and I get the same error. It seems like the issue is related to passing a list objects as a function parameter.</p>

<p>I should also clarify that we are using Python 2.5.2.</p>

<p>Two changes had to be made for this to work (thanks to Scott Mermelstein):</p>

<p>In the main loop, <code>threadScheduler(strlist)</code> had to be changed to <code>threadScheduler(*strlist)</code>. I verified this by adding the following line to the <code>threadScheduler()</code> function:</p>

<pre><code>print 'in scheduler', stringlist[0].f()
</code></pre>

<p>and was able to successfully call <code>f()</code> only after I added the asterisk. I'm unsure of why the argument had to be passed this way, though.</p>

<p>Additionally, I had to change </p>

<pre><code>thread = threading.Thread(name='heartbeat', target=threadWorkLoad, args=(stringlist,))
</code></pre>

<p>to</p>

<pre><code>thread = threading.Thread(name='heartbeat', target=threadWorkLoad, args=tuple(stringlist))
</code></pre>

<p>Then I was able to successfully call <code>f()</code> for each object in the list in the <code>threadWorkLoad()</code> function.</p>

python multithreading