gpt4 book ai didi

Python线程死锁

转载 作者:太空宇宙 更新时间:2023-11-04 06:36:09 25 4
gpt4 key购买 nike

我有两个线程(生产者和消费者),我与 Queue 共享数据。问题是,当我强行中止生产者时,消费者有时会锁定。

我在文档中读到取消带有队列的线程可能会破坏队列并导致死锁。我没有明确获取任何锁,但阅读 Queue.py 的源代码说 putget 正在这样做。

拜托,有谁知道当我中止线程时,它可能在 get/put 的中间,即使用锁然后不释放它?我该怎么办?有时我需要提前终止生产者。使用进程而不是线程会有什么不同吗?

最佳答案

也许这会有所帮助:

import threading

class MyQueue:
def __init__(self):
self.tasks = []
self.tlock = threading.Semaphore(0)
self.dlock = threading.Lock()
self.aborted = False

def put(self, arg):
try:
self.dlock.acquire()
self.tasks.append(arg)
finally:
self.dlock.release()
self.tlock.release()

def get(self):
if self.aborted:
return None
self.tlock.acquire()
if self.aborted:
self.tlock.release()
return None
try:
self.dlock.acquire()
if self.tasks:
return self.tasks.pop()
else: # executed abort
return None
finally:
self.dlock.release()

def abort(self):
self.aborted = True
self.tlock.release()

# TESTING

mq = MyQueue()
import sys

def tlog(line):
sys.stdout.write("[ %s ] %s\n" % (threading.currentThread().name, line))
sys.stdout.flush()

def reader():
arg = 1
while arg is not None:
tlog("start reading")
arg = mq.get()
tlog("read: %s" % arg)
tlog("END")

import time, random
def writer():
try:
pos = 1
while not mq.aborted:
x = random.random() * 5
tlog("writer sleep (%s)" % x)
pending = x
while pending > 0:
tosleep = min(0.5, pending)
if mq.aborted:
return
time.sleep(tosleep)
pending -= tosleep

tlog("write: %s" % x)
mq.put("POS %s val=%s" % (pos, x))
pos += 1
finally:
tlog("writer END")

def testStart():
try:
for i in xrange(9):
th = threading.Thread(None, reader, "reader %s" % i, (), {}, None)
th.start()
for i in xrange(3):
th = threading.Thread(None, writer, "writer %s" % i, (), {}, None)
th.start()
time.sleep(30) # seconds for testing
finally:
print "main thread: abort()"
mq.abort()

if __name__ == "__main__":
testStart()

关于Python线程死锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10760948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com