Created
June 20, 2012 09:40
-
-
Save hpk42/2959084 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
import time | |
from execnet.threadpool import WorkerPool | |
def safe_terminate(timeout, list_of_paired_functions): | |
workerpool = WorkerPool(len(list_of_paired_functions)*2) | |
def termkill(termfunc, killfunc): | |
termreply = workerpool.dispatch(termfunc) | |
try: | |
termreply.get(timeout=timeout) | |
except IOError: | |
pass | |
killfunc() | |
replylist = [] | |
for termfunc, killfunc in list_of_paired_functions: | |
reply = workerpool.dispatch(termkill, termfunc, killfunc) | |
replylist.append(reply) | |
for reply in replylist: | |
reply.get() | |
def test_safe_terminate(): | |
def term(): | |
print "waiting-termination" | |
time.sleep(3) | |
def kill(): | |
print "killed" | |
safe_terminate(1, [(term, kill)] * 10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment