Ragav
Ragav

Reputation: 962

Threading and information passing -- how to

To reframe from confusion i have edited the question:

one.py

import threading
count = 5
dev = threading.Thread(name='dev', target=dev,args=(workQueue,count,))
dev.setDaemon(True)
dev.start()
workQueue = Queue.Queue(10)
queueLock.acquire()
workQueue.put(word)
queueLock.release()
count = 3
time.sleep(2)
count = 5

but my confusion here is I am able to put and get values from queue between threads but in case of count it does not reflect.

Why is that?
What is point am actually missing here?

class dev ( threading.Thread ):
    def test(self):
        while 1:
            print count
            print self.EPP_Obj
            queueLock.acquire()
            if not self.workQueue.empty():
                data = self.workQueue.get()
                print data
                queueLock.release()
            else:
                queueLock.release()

    def __init__(self, workQueue, EPP_Obj):
        threading.Thread.__init__(self)
        self.workQueue = workQueue
        self.EPP_Obj = EPP_Obj

Upvotes: 6

Views: 15235

Answers (2)

Rik Poggi
Rik Poggi

Reputation: 29302

Let's start with an example:

The Thread subclass:

import threading

class Dev(threading.Thread):

    def __init__(self, workQueue, queueLock, count):
        super(Dev, self).__init__()   # super() will call Thread.__init__ for you
        self.workQueue = workQueue
        self.queueLock= queueLock
        self.count = count

    def run(self):  # put inside run your loop
        data = ''
        while 1:
            with self.queueLock:
                if not self.workQueue.empty():
                    data = self.workQueue.get()
                    print data
                    print self.count

            if data == 'quit':
                break

The with statement is a smart way to acquire and release a lock, take a look at the doc.

Now the running code:

import Queue
import time

work_q = Queue.Queue()     # first create your "work object"
q_lock = threading.Lock()
count = 1

dev = Dev(work_q, q_lock, count)  # after instantiate like this your Thread
dev.setDaemon(True)
dev.start()

time.sleep(1)
with q_lock:
    work_q.put('word')
# word
# 1

time.sleep(1)
count = 10
with q_lock:
    work_q.put('dog')
# dog
# 1

count = 'foo'
with q_lock:
    work_q.put('quit')
# quit
# 1

dev.join()   # This will prevent the main to exit
             # while the dev thread is still running

With the code above we have a clear example on how self.count stays unchanged no matter what we do to count.
The reason of this behaviour is that calling:

dev = Dev(work_q, q_lock, count)

or

dev = Dev(work_q, q_lock, 1)

is the same thing.

Arnold Moon Showed you a way to change self.count. Adjusting that to our example:

class Dev(threading.Thread):

    def __init__(self, workQueue, queueLock, count):
        super(Dev, self).__init__()
        self.workQueue = workQueue
        self.queueLock= queueLock
        self.count = count

    def set_count(self, value):
        self.count = value

    def run(self):
        data = ''
        while 1:
            with self.queueLock:
                if not self.workQueue.empty():
                    data = self.workQueue.get()
                    print data
                    print self.count

            if data == 'quit':
                break

Calling set_count in our running code will change the value of self.count:

time.sleep(1)
with q_lock:
    work_q.put('word')
# word
# 1

time.sleep(1)
count = dev.count + 9
dev.set_count(count)
with q_lock:
    work_q.put('dog')
# dog
# 10

count = 'foo'
with q_lock:
    work_q.put('quit')
# quit
# 10
dev.join()

I hope this will help you clarify some doubts.

Upvotes: 8

Arnold Moon
Arnold Moon

Reputation: 88

I hope this will help you. I think you don't know which way you need to use. there are some ways for multi-threading in python. i introduce the way which use class. you run below codes. you would understand.

main.py

import stringRepeater
import Queue

workqueue = Queue.Queue()
workqueue.put('test1')
workqueue.put('test2')
workqueue.put('test3')

th = stringRepeater.stringRepeater(workqueue,5)
th.start()
print '----daemon is on ----'
th.setCount(3)
workqueue.put('test4')
workqueue.put('test5')

stringRepeater.py

import threading

class stringRepeater(threading.Thread):
    def __init__(self, workQueue, count):
        threading.Thread.__init__(self)
        self.workQueue = workQueue
        self.repeatCount = count

    def run(self):
        while True:
            teststring = self.workQueue.get()
            for i in range(self.repeatCount):
                print teststring

    def setCount(self, newcount):
        self.repeatCount = newcount

Upvotes: 4

Related Questions