简体   繁体   中英

Pyside: How can i know which thread emitted the signal “finished”?

I start some threads und would like to know which thread is finished:

...
...
def start_ten_threads(self):
    self.workers = []

    for i in range(0, 10):
        self.workers.append(thread_worker(i, self))

        ...    
        ...

    for k in range(0, 10):
        self.workers[k].finished.connect(lambda: self.print_finished("WorkerThread %d finished" % k))
        self.workers[k].start()

@Slot(str)
def print_finished(self, str):
    print(str)

I get for each thread the same number 9 (last value of k). Why? Can i use parameters on this way with signal "finished"? I have already read some stuff about QSignalMapper. Is there another way here?

If i use self.sender() i get the point to the thread, but i don't know which thread number it is.

For the lambda, try:

lambda k=k: self.print_finished("WorkerThread %d finished" % k)
       ^^^

With this syntax the value of k at the time the lambda was created will be captured and used by the lambda.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM