线程和信息传递 - 如何

Rag*_*gav 5 python python-multithreading

为了重新构思混乱,我编辑了这个问题:

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
Run Code Online (Sandbox Code Playgroud)

但我在这里的困惑是我能够在线程之间放入和获取队列中的值,但是在计数的情况下它不会反映出来.

这是为什么?
我实际上错过了什么意思?

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
Run Code Online (Sandbox Code Playgroud)

Rik*_*ggi 7

让我们从一个例子开始:

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
Run Code Online (Sandbox Code Playgroud)

with声明是一种获取和释放锁定的智能方法,请查看该文档.

现在运行代码:

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
Run Code Online (Sandbox Code Playgroud)

通过上面的代码,我们有一个明确的例子,说明self.count无论我们做什么,如何保持不变count.
这种行为的原因是调用:

dev = Dev(work_q, q_lock, count)
Run Code Online (Sandbox Code Playgroud)

要么

dev = Dev(work_q, q_lock, 1)
Run Code Online (Sandbox Code Playgroud)

是一回事.

Arnold Moon告诉你一种改变的方法self.count.调整到我们的例子:

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
Run Code Online (Sandbox Code Playgroud)

调用set_count我们正在运行的代码将改变以下值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()
Run Code Online (Sandbox Code Playgroud)

我希望这能帮助你澄清一些疑问.

  • 队列的设计不是线程安全的吗,因此与列表或非线程安全的东西相比,锁是多余的? (2认同)