Harry
Harry

Reputation: 2952

python Thread.name is printing last thread created name

I'm a newbie to Python and learning about threads. I have created a sample Producer-Consumer code wherein I add a movie to a list in Producer thread and pop the front element from the same list in Consumer thread. The problem is while printing the items of the movie List along with thread name I'm getting incorrect thread name in Producer thread. This is my code

Producer.py

from threading import Thread
from threading import RLock
import time


class Producer(Thread):
    def __init__(self):
        Thread.__init__(self)
        Thread.name = 'Producer'

        self.movieList = list()
        self.movieListLock = RLock()


    def printMovieList(self):
        self.movieListLock.acquire()

        if len(self.movieList) > 0:
            for movie in self.movieList:
                print(Thread.name, movie)

            print('\n')

        self.movieListLock.release()


    def pushMovieToList(self, movie):
        self.movieListLock.acquire()

        self.movieList.append(movie)
        self.printMovieList()

        self.movieListLock.release()


    def run(self):
        for i in range(6):
            self.pushMovieToList('Avengers' + str(i + 1))
            time.sleep(1)

Consumer.py

from threading import Thread
import time


class Consumer(Thread):

    def __init__(self):
        Thread.__init__(self)
        Thread.name = 'Consumer'

        self.objProducer = None


    def popMovieFromList(self):
        self.objProducer.movieListLock.acquire()

        if len(self.objProducer.movieList) > 0:
            movie = self.objProducer.movieList.pop(0)
            print(Thread.name, ':', movie)
            print('\n')

        self.objProducer.movieListLock.release()


    def run(self):
        while True:
            time.sleep(1)
            self.popMovieFromList()

Main.py

from Producer import *
from Consumer import *


def main():
    objProducer = Producer()
    objConsumer = Consumer()

    objConsumer.objProducer = objProducer

    objProducer.start()
    objConsumer.start()

    objProducer.join()
    objConsumer.join()


main()

Upvotes: 1

Views: 829

Answers (1)

littlestone
littlestone

Reputation: 11

I am not sure whether you solve this problem.
Hope my answer will be helpful.
You can check the document of threading.

Here it says that Thread.name may set same name for multiple thread.

name
A string used for identification purposes only. It has no semantics. Multiple threads may be given the same name. The initial name is set by the constructor.

I think Thread.name is a static variable so it shares in different thread. If you want to set name of thread, you can set it in thread object like this:

class Producer(Thread):
def __init__(self):
    Thread.__init__(self)
    self.name= 'Producer'

and get it by threading.current_thread().name.

if len(self.movieList) > 0:
    for movie in self.movieList:
    print(threading.current_thread().name, movie)

Hope you enjoy it!

Upvotes: 1

Related Questions