首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >线程化和信息传递--如何

线程化和信息传递--如何
EN

Stack Overflow用户
提问于 2012-02-08 16:55:30
回答 2查看 12.3K关注 0票数 5

为了避免混淆,我编辑了这个问题:

one.py

代码语言:javascript
复制
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

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

为什么会这样呢?

我在这里到底遗漏了什么?

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-02-09 04:39:08

让我们从一个例子开始:

Thread子类:

代码语言:javascript
复制
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

with语句是获取和释放锁的一种聪明方法,请看一下doc

现在运行的代码如下:

代码语言:javascript
复制
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

有了上面的代码,我们就有了一个清晰的例子,说明无论我们对count做什么,self.count都保持不变。

这种行为的原因是调用:

代码语言:javascript
复制
dev = Dev(work_q, q_lock, count)

代码语言:javascript
复制
dev = Dev(work_q, q_lock, 1)

都是一回事。

Arnold Moon向您展示了一种更改self.count的方法。将其调整为我们的示例:

代码语言:javascript
复制
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

在运行代码中调用set_count将更改self.count的值

代码语言:javascript
复制
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()

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

票数 7
EN

Stack Overflow用户

发布于 2012-02-08 17:37:22

我希望这能对你有所帮助。我想你不知道你需要用哪种方式。在python中有一些多线程的方法。我介绍了类的使用方法。你在下面的代码中运行。你会明白的。

main.py

代码语言:javascript
复制
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

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9190169

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档