0

I wrote the following code (simplified) to add objects to an initially empty list (listOfElements, which is a global variable). Yet, when I print this list in the end, it remains empty. What did I do wrong?

import threading
def addElement(listOfElements):
 for k in range(10):
 listOfElements.append(k)
listOfElements = []
import threading
def addElement(listOfElements,otherList):
 for k in range(10):
 listOfElements.append(k)
listOfElements = []
threadsElts = []
for i in range(10):
 threadsElts.append(threading.Thread(target=addElement,args=(listOfElements,otherList)))
for t in threadsElts:
 t.start()
for t in threadsElts:
 t.join()
threadsElts = []
for i in range(10):
 threadsElts.append(threading.Thread(target=addElement,args=(listOfElements,otherList)))
for t in threadsElts:
 t.start()
for t in threadsElts:
 t.join()
asked May 13, 2014 at 9:22
2
  • Could you please review the indentation; it's important in Python. Commented May 13, 2014 at 9:25
  • Is your code running? Mind even doing required imports, just one or two lines and it helps other so check it quickly without guess work. Commented May 13, 2014 at 9:28

2 Answers 2

2

The problem is with the code. with your code, i get this error:-

Exception in thread Thread-1:
Traceback (most recent call last):
 File "/usr/local/akamai/lib/python2.7/threading.py", line 551, in __bootstrap_inner
 File "/usr/local/akamai/lib/python2.7/threading.py", line 504, in run
TypeError: addElement() takes exactly 1 argument (0 given)
Exception in thread Thread-2:
Traceback (most recent call last):
 File "/usr/local/akamai/lib/python2.7/threading.py", line 551, in __bootstrap_inner
 File "/usr/local/akamai/lib/python2.7/threading.py", line 504, in run
TypeError: addElement() takes exactly 1 argument (0 given)
Exception in thread Thread-3:
Traceback (most recent call last):
 File "/usr/local/akamai/lib/python2.7/threading.py", line 551, in __bootstrap_inner
 File "/usr/local/akamai/lib/python2.7/threading.py", line 504, in run
TypeError: addElement() takes exactly 1 argument (0 given)
Exception in thread Thread-4:
Traceback (most recent call last):
 File "/usr/local/akamai/lib/python2.7/threading.py", line 551, in __bootstrap_inner
 File "/usr/local/akamai/lib/python2.7/threading.py", line 504, in run
TypeError: addElement() takes exactly 1 argument (0 given)
Exception in thread Thread-5:
Traceback (most recent call last):
 File "/usr/local/akamai/lib/python2.7/threading.py", line 551, in __bootstrap_inner
 File "/usr/local/akamai/lib/python2.7/threading.py", line 504, in run
TypeError: addElement() takes exactly 1 argument (0 given)
Exception in thread Thread-6:
Traceback (most recent call last):
 File "/usr/local/akamai/lib/python2.7/threading.py", line 551, in __bootstrap_inner
 File "/usr/local/akamai/lib/python2.7/threading.py", line 504, in run
TypeError: addElement() takes exactly 1 argument (0 given)
Exception in thread Thread-7:
Traceback (most recent call last):
 File "/usr/local/akamai/lib/python2.7/threading.py", line 551, in __bootstrap_inner
 File "/usr/local/akamai/lib/python2.7/threading.py", line 504, in run
TypeError: addElement() takes exactly 1 argument (0 given)
Exception in thread Thread-8:
Traceback (most recent call last):
 File "/usr/local/akamai/lib/python2.7/threading.py", line 551, in __bootstrap_inner
 File "/usr/local/akamai/lib/python2.7/threading.py", line 504, in run
TypeError: addElement() takes exactly 1 argument (0 given)
Exception in thread Thread-9:
Traceback (most recent call last):
 File "/usr/local/akamai/lib/python2.7/threading.py", line 551, in __bootstrap_inner
 File "/usr/local/akamai/lib/python2.7/threading.py", line 504, in run
TypeError: addElement() takes exactly 1 argument (0 given)
Exception in thread Thread-10:
Traceback (most recent call last):
 File "/usr/local/akamai/lib/python2.7/threading.py", line 551, in __bootstrap_inner
 File "/usr/local/akamai/lib/python2.7/threading.py", line 504, in run
TypeError: addElement() takes exactly 1 argument (0 given)

It can be resolved if you pass listOfElements like this:-

threadsElts.append(threading.Thread(target=addElement,args=(listOfElements,)))

The code runs fine after making the above change.

Also, keep in mind that you are modifying a variable without taking any locks, so your code might corrupt your data. You might want to go through this link: http://effbot.org/zone/thread-synchronization.htm for better understanding the problem

answered May 13, 2014 at 9:28
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the link. I think the appending operation is a thread-safe operation (cf Atomic Operations paragraph)
As far as appending being a thread safe operation, please go through this link: stackoverflow.com/questions/6319207/are-lists-thread-safe
1

your problem is simple, TypeError: addElement() takes exactly 1 argument (0 given).

it tell us your problem is the function's param, while your param is given in the thread args=(listOfElements), it looks good, but syntax error.

in python, the tuple is special, look this:

a = () # a has no element
a = (1) # error
a = (1,) # yes, a has one element, the dot cannot be missing, special
a = (1,2) # yes, a has two

You can try it.

answered May 13, 2014 at 9:43

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.