Pergunta

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()
Foi útil?

Solução

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

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top