python - ThreadPoolExecutor hangs when call futur result in a futur callback -
i using "requests-futures" package , call asynchronous get/post in asynchronous get/post result callback (add_done_callback on futur result). sometimes, code hangs. after many investigation hours, can reproduce lock minimal code:
from concurrent.futures import threadpoolexecutor import time pool = threadpoolexecutor(max_workers=10) def f(_): time.sleep(0.1) # try force context switch x = pool.submit(lambda: none) print "1" x.result() print "2" def main(): x = pool.submit(lambda : none) x.add_done_callback(f) print "3" x.result() print "4" print "===" main() if run peace of code in bash loop:
$> while true; python code.py; done; the program hangs every times "trace":
(...) === 1 2 3 4 === 3 4 1 if break ctrl^c, have following stack trace:
^cerror in atexit._run_exitfuncs: traceback (most recent call last): file "/usr/lib/python2.7/atexit.py", line 24, in _run_exitfuncs func(*targs, **kargs) file "/home/yienyien/angus/test/futur/env/local/lib/python2.7/site- packages/concurrent/futures/thread.py", line 46, in _python_exit t.join(sys.maxint) file "/usr/lib/python2.7/threading.py", line 951, in join self.__block.wait(delay) file "/usr/lib/python2.7/threading.py", line 359, in wait _sleep(delay) keyboardinterrupt error in sys.exitfunc: traceback (most recent call last): file "/usr/lib/python2.7/atexit.py", line 24, in _run_exitfuncs func(*targs, **kargs) file "/home/yienyien/angus/test/futur/env/local/lib/python2.7/site- packages/concurrent/futures/thread.py", line 46, in _python_exit t.join(sys.maxint) file "/usr/lib/python2.7/threading.py", line 951, in join self.__block.wait(delay) file "/usr/lib/python2.7/threading.py", line 359, in wait _sleep(delay) keyboardinterrupt somebody explain me happening ? check the possible deadlocks in concurrent.futures module, not think matches.
thank you.
tasks submitted fixed-sized thread pool may not call blocking operations future.result(). leads specific kind of deadlock, called "thread starvation". using time.sleep() switches thread off service , increases probability of thread starvation.
Comments
Post a Comment