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()
teaLeef
  • 1,879
  • 2
  • 16
  • 26

2 Answers2

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

GodMan
  • 2,561
  • 2
  • 24
  • 40
  • thanks for the link. I think the appending operation is a thread-safe operation (cf Atomic Operations paragraph) – teaLeef May 13 '14 at 09:41
  • As far as appending being a thread safe operation, please go through this link: http://stackoverflow.com/questions/6319207/are-lists-thread-safe – GodMan May 13 '14 at 09:53
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.

BBDXF
  • 31
  • 3