5

混乱からリフレームするために、私は質問を編集しました:

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

しかし、ここでの私の混乱は、スレッド間のキューから値を入れたり取得したりできることですが、カウントの場合は反映されません。

何故ですか?
ここで実際に欠けているポイントは何ですか?

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

2 に答える 2

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

この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

self.count上記のコードを使用すると、何をしても変更されないという明確な例がありcountます。
この動作の理由は、次の呼び出しです。

dev = Dev(work_q, q_lock, count)

また

dev = Dev(work_q, q_lock, 1)

同じことです。

アーノルドムーンはあなたに変更する方法を示しました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

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()

これがいくつかの疑問を明らかにするのに役立つことを願っています。

于 2012-02-08T20:39:08.027 に答える
4

これがお役に立てば幸いです。どちらの使い方がいいのかわからないと思います。Pythonでマルチスレッド化する方法はいくつかあります。クラスの使い方を紹介します。以下のコードを実行します。あなたは理解するでしょう。

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
于 2012-02-08T09:37:22.277 に答える