Data la Python documentation per Thread.run()
:python override threading.Thread.run()
You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.
ho costruito il seguente codice:
class DestinationThread(threading.Thread):
def run(self, name, config):
print 'In thread'
thread = DestinationThread(args = (destination_name, destination_config))
thread.start()
Ma quando eseguo esso, ricevo il seguente errore :
Exception in thread Thread-1:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py", line 522, in __bootstrap_inner
self.run()
TypeError: run() takes exactly 3 arguments (1 given)
sembra che mi manca qualcosa di ovvio, ma i vari esempi Ho visto lavorare con questa metodologia. In definitiva sto provando a passare solo la stringa e il dizionario nel thread, se il Costruttore non è la strada giusta, ma piuttosto per creare una nuova funzione per impostare i valori prima di iniziare il thread, sono aperto a quello.
Qualche suggerimento su come realizzare al meglio questo?
Questa era un'informazione chiave che mi mancava, ed è per questo che l'ho accettata, ma nel mio caso, in realtà sto estendendo la classe aggiungendo le funzioni necessarie per estrarre i dati dal thread. –
Mille volte sì. Devo spiegare questo molto agli sviluppatori Java. –
Non hai nemmeno bisogno di argomenti = (arg1, arg2). Basta fare target = functools.partial (myfunc, arg1, arg2). – Phob