Fix killing greenlets gevent exception
This commit is contained in:
parent
ddbd5c7b19
commit
6bd49e8aff
3 changed files with 15 additions and 5 deletions
|
@ -6,13 +6,19 @@ from Config import config
|
||||||
|
|
||||||
# Non fatal exception
|
# Non fatal exception
|
||||||
class Notify(Exception):
|
class Notify(Exception):
|
||||||
def __init__(self, message):
|
def __init__(self, message=None):
|
||||||
self.message = message
|
if message:
|
||||||
|
self.message = message
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.message
|
return self.message
|
||||||
|
|
||||||
|
|
||||||
|
# Gevent greenlet.kill accept Exception type
|
||||||
|
def createNotifyType(message):
|
||||||
|
return type("Notify", (Notify, ), {"message": message})
|
||||||
|
|
||||||
|
|
||||||
def formatExceptionMessage(err):
|
def formatExceptionMessage(err):
|
||||||
err_type = err.__class__.__name__
|
err_type = err.__class__.__name__
|
||||||
if err.args:
|
if err.args:
|
||||||
|
@ -101,6 +107,8 @@ import time
|
||||||
|
|
||||||
|
|
||||||
num_block = 0
|
num_block = 0
|
||||||
|
|
||||||
|
|
||||||
def testBlock():
|
def testBlock():
|
||||||
global num_block
|
global num_block
|
||||||
logging.debug("Gevent block checker started")
|
logging.debug("Gevent block checker started")
|
||||||
|
@ -111,6 +119,8 @@ def testBlock():
|
||||||
logging.debug("Gevent block detected: %.3fs" % (time.time() - last_time - 1))
|
logging.debug("Gevent block detected: %.3fs" % (time.time() - last_time - 1))
|
||||||
num_block += 1
|
num_block += 1
|
||||||
last_time = time.time()
|
last_time = time.time()
|
||||||
|
|
||||||
|
|
||||||
gevent.spawn(testBlock)
|
gevent.spawn(testBlock)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -226,7 +226,7 @@ class Worker(object):
|
||||||
def skip(self, reason="Unknown"):
|
def skip(self, reason="Unknown"):
|
||||||
self.manager.log.debug("%s: Force skipping (reason: %s)" % (self.key, reason))
|
self.manager.log.debug("%s: Force skipping (reason: %s)" % (self.key, reason))
|
||||||
if self.thread:
|
if self.thread:
|
||||||
self.thread.kill(exception=Debug.Notify("Worker skipping (reason: %s)" % reason))
|
self.thread.kill(exception=Debug.createNotifyType("Worker skipping (reason: %s)" % reason))
|
||||||
self.start()
|
self.start()
|
||||||
|
|
||||||
# Force stop the worker
|
# Force stop the worker
|
||||||
|
@ -234,6 +234,6 @@ class Worker(object):
|
||||||
self.manager.log.debug("%s: Force stopping (reason: %s)" % (self.key, reason))
|
self.manager.log.debug("%s: Force stopping (reason: %s)" % (self.key, reason))
|
||||||
self.running = False
|
self.running = False
|
||||||
if self.thread:
|
if self.thread:
|
||||||
self.thread.kill(exception=Debug.Notify("Worker stopped (reason: %s)" % reason))
|
self.thread.kill(exception=Debug.createNotifyType("Worker stopped (reason: %s)" % reason))
|
||||||
del self.thread
|
del self.thread
|
||||||
self.manager.removeWorker(self)
|
self.manager.removeWorker(self)
|
||||||
|
|
|
@ -20,5 +20,5 @@ class GreenletManager:
|
||||||
|
|
||||||
def stopGreenlets(self, reason="Stopping all greenlets"):
|
def stopGreenlets(self, reason="Stopping all greenlets"):
|
||||||
num = len(self.greenlets)
|
num = len(self.greenlets)
|
||||||
gevent.killall(list(self.greenlets), Debug.Notify(reason), block=False)
|
gevent.killall(list(self.greenlets), Debug.createNotifyType(reason), block=False)
|
||||||
return num
|
return num
|
||||||
|
|
Loading…
Reference in a new issue