【发布时间】:2018-10-12 17:24:57
【问题描述】:
我的程序有两个线程 - 第一个用于接收字典列表形式的数据,第二个线程用于将值存储在数据库中。
buffer = collections.deque(maxlen=10)
def process_ticks(bufferarg):
while True:
for i in bufferarg:
#code to insert data in a database
#this tread receives the data and dumps it into a deque with a specified length (so it can function as a circular buffer)
t1 = threading.Thread(target=socket.connect)
#this threads accesses the deque and processes the data
t2 = threading.Thread(target=process_ticks(buffer))
t1.start()
t2.start()
但是,当我运行代码时,我收到“deque is being mutated”错误。
另外,如何确保线程无限运行,但process_ticks 不会从双端队列插入相同的数据两次?
【问题讨论】:
-
请注意,如前所述,从未启动过线程:
process_ticks(buffer)已经启动了无限循环,而不是将其推送到线程。这会阻塞主线程。它应该改为t2 = threading.Thread(target=process_ticks, args=(buffer,))。 -
@MisterMiyagi 非常感谢,伙计!
标签: python python-3.x multithreading