Skip to content

Instantly share code, notes, and snippets.

@hpk42
Created June 20, 2012 09:40
Show Gist options
  • Save hpk42/2959084 to your computer and use it in GitHub Desktop.
Save hpk42/2959084 to your computer and use it in GitHub Desktop.
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