Threading and information passing -- how to(线程和信息传递--如何)
                            本文介绍了线程和信息传递--如何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
                        
                        问题描述
为了避免混淆,我编辑了问题:
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
推荐答案
让我们从一个例子开始:
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语句是获取和释放锁的智能方法,请查看doc。
现在运行代码:
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如何保持不变。此行为的原因是调用:
dev = Dev(work_q, q_lock, count)
或
dev = Dev(work_q, q_lock, 1)
是一样的。
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
在我们的运行代码中调用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()
我希望这将帮助您澄清一些疑虑。
这篇关于线程和信息传递--如何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
				 沃梦达教程
				
			本文标题为:线程和信息传递--如何
 
				
         
 
            
        基础教程推荐
             猜你喜欢
        
	     - 求两个直方图的卷积 2022-01-01
- 包装空间模型 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
				 
				 
				 
				