这个双端队列在Python中是线程安全的吗?
我无法确定以下双端队列是否是线程安全的。
简而言之,我创建了一个带有双端队列的类,该类在新线程中每 1 秒显示一次其内容(因此在打印时不会暂停主程序)。
双端队列是从主线程填充的,所以基本上应该有碰撞的机会。
然而,双端队列是使用类方法填充的,因此本质上它是从实例本身内部访问的,因此是从同一个线程访问的。
下面是简化的代码:
import threading
import time
from collections import deque
class MyQueue(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.q = deque()
self.start()
def run(self):
# pop out queue items every 1 sec
# (please ignore empty deque for now)
while True:
print self.q.popleft()
time.sleep(1)
def add_to_q(self, val):
# this function is called from outside
self.q.append(val)
# main
# fill the queue with values
qu = MyQueue()
for i in range(1:100):
qu.add_to_q(i)
那么,虽然从队列中添加和删除项目是在实例内部进行的,但是从实例外部调用添加函数是否存在风险?
编辑:
由于我需要修改双端队列中的项目,因此我必须使用双端队列。我所做的是:将其旋转()到给定的项目,将其弹出,修改,将其推回原处,然后旋转()将其返回到原始位置。
除非我找到一种方法来实现修改队列中的项目,否则我将不得不坚持使用 Deque
I can't decide whether the following deque is thread-safe.
In short, I've created a class with a deque that displays its contents every 1 sec in a new thread (so it won't pause the main program while printing).
The deque is filled from the main thread, so basically there SHOULD be a chance of collision.
HOWEVER, the deque is filled using a class method, so essentially it is accessed from within the instance itself, therefore from the same thread.
Here's the simplified code:
import threading
import time
from collections import deque
class MyQueue(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.q = deque()
self.start()
def run(self):
# pop out queue items every 1 sec
# (please ignore empty deque for now)
while True:
print self.q.popleft()
time.sleep(1)
def add_to_q(self, val):
# this function is called from outside
self.q.append(val)
# main
# fill the queue with values
qu = MyQueue()
for i in range(1:100):
qu.add_to_q(i)
So, although adding and removing items from queue take place inside the instance, is there a risk due to the adding function being called from outside the instance?
EDIT:
Since I need to modify items in my deque, I had to use Deque. What I do is: roatate() to the given item, pop it out, modify, push it back in and rotate() it back to its original position.
Unless I find a way of implementing modifying items in a Queue, I'll have to stick to Deque
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
双端队列是线程安全的(http://docs.python.org/library/collections .html#deque-objects) 用于从相对侧追加和弹出。 在这里,文档只提到了append()和popleft () 是线程安全的。
队列本身有一个线程安全的实现。因此,除非您有一些奇怪的要求,否则您应该使用它。
Deque is thread-safe (http://docs.python.org/library/collections.html#deque-objects) for appends and pops from opposite sides. Beneath here, the docs only mention that append() and popleft() are thread-safe.
There is a thread-safe implementation of the Queue itself. So you should be using it unless you have some strange requirements.
Deque 作者在这里。
MyQueue() 类对我来说看起来是正确的(至少就线程安全问题而言)。
append()
和popleft()
方法都是原子的。该代码确实需要 EAFP 逻辑来处理输入为空的情况:
Deque author here.
The MyQueue() class looks correct to me (at least with respect to the question of thread-safety).
The
append()
andpopleft()
methods are both atomic.The code does need EAFP logic to handle the case where the input is empty:
有关信息,有一个针对双端队列线程安全引用的 Python 票证 (https://bugs.python.org/issue15329)。
标题“澄清哪些双端队列方法是线程安全的”,底线是:
无论如何,如果您不是 100% 确定并且您更喜欢可靠性而不是性能,只需为
print self.q.popleft()
和self.q.append(val)< 添加一个类似的 Lock /代码>;)
For information there is a Python ticket referenced for deque thread-safety (https://bugs.python.org/issue15329).
Title "clarify which deque methods are thread-safe", bottom line is:
Anyway, if you are not 100% sure and you prefer reliability over performance, just put a like Lock for
print self.q.popleft()
andself.q.append(val)
;)