我有两个线条(制片人和消费者),用ueue
分享数据。问题是,当我强行中止制片人时,消费者有时会锁上。
我在文档中读到,取消队列线可能会腐蚀队列并造成僵局。 我不明显获得任何锁, 但读取 Quue.py 的来源时表示 < code> put 和 < code>get 正在这样做 。
请, 有人知道, 当我中止线条时, 它可能在 get
/ put
put 中间, 也就是说, 使用锁而不释放它? 我该怎么办? 我有时需要提前终止制片人。 使用程序而不是线条会有什么不同吗?
我有两个线条(制片人和消费者),用ueue
分享数据。问题是,当我强行中止制片人时,消费者有时会锁上。
我在文档中读到,取消队列线可能会腐蚀队列并造成僵局。 我不明显获得任何锁, 但读取 Quue.py 的来源时表示 < code> put 和 < code>get 正在这样做 。
请, 有人知道, 当我中止线条时, 它可能在 get
/ put
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
" % (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()
您的僵局很可能是由于没有完成线索。 如果您有 Linux, 您可以使用 < a href=" http://pypi. python.org/ pypi/ pyrasite" rel=" nofollow" > pyrasite a > 的输入器打印回溯跟踪( 您知道程序挂在哪里) 。
If you are using any locks in your signal handler - then probably this is your deadlock (that s a bit complicated, please ask if you want explanation for that)
创建进程而不是线索肯定会改变情况,但记住,任何数据交换和同步都非常复杂。
Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...
I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...
Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...
Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...
I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...
Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...
Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...
I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...