Information transfer and transfer - how

To rephrase from confusion, I 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 that I can put and receive values ​​from the queue between threads, but in the case of counting it does not reflect.

Why is this? Which point is 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 
+4
source share
2 answers

Let's start with an example:

Subclass Thread :

 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 get and release a lock, see doc .

Now the current 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 above code, we have a clear example of how self.count remains unchanged no matter what we do with count .
The reason for this behavior is that the call:

 dev = Dev(work_q, q_lock, count) 

or

 dev = Dev(work_q, q_lock, 1) 

- same.

Arnold Moon self.count way to change self.count . Applying this 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 current 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() 

Hope this helps you clarify some doubts.

+6
source

Hope this helps you. I think you do not know which way you need to use. There are several ways to multithread in python. I present a way to use a class. you run the codes below. 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 
+4
source

Source: https://habr.com/ru/post/1395375/


All Articles