Threading and information passing — how to

∥☆過路亽.° 提交于 2019-12-04 08:05:27
Rik Poggi

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.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!