擅长:python、mysql、java
<p>你在这里所经历的,就是你已经正确地称之为<a href="https://stackoverflow.com/questions/34512/what-is-a-deadlock">deadlock</a>。启动下一个线程并等待它的第一个线程持有一个<code>lock</code>,在等待同一个{<cd1>}被释放时,所有后续任务都将在该线程上死锁(在您的情况下永远不会)。我建议您在任务中启动自己的线程,而不是使用池,类似于:</p>
<pre><code>import concurrent.futures
import threading
class TaskWrapper(threading.Thread):
def __init__(self, depth, *args, **kwargs):
self._depth = depth
self._result = None
super(TaskWrapper, self).__init__(*args, **kwargs)
def run(self):
self._result = task(self._depth)
def get(self):
self.join()
return self._result
e = concurrent.futures.ThreadPoolExecutor(max_workers=10)
def task(depth):
print 'started depth %d' % (depth, )
if depth > 10:
return depth
else:
t = TaskWrapper(depth + 1)
t.start()
return t.get()
f = e.submit(task, 0)
print f.result()
</code></pre>