[Checkins] SVN: zc.thread/trunk/ Thread names now include a function's module name.
jim
cvs-admin at zope.org
Tue Jan 1 21:08:22 UTC 2013
Log message for revision 128984:
Thread names now include a function's module name.
Changed:
U zc.thread/trunk/README.txt
U zc.thread/trunk/src/zc/thread/__init__.py
-=-
Modified: zc.thread/trunk/README.txt
===================================================================
--- zc.thread/trunk/README.txt 2013-01-01 20:41:06 UTC (rev 128983)
+++ zc.thread/trunk/README.txt 2013-01-01 21:08:21 UTC (rev 128984)
@@ -41,6 +41,13 @@
the thread's ``exception`` attribute. (This feature was inspired by
the same feature in gevent greenlets.)
+- If a thread raises an exception (subclass of Exception), the
+ exception is logged and a traceback is printed to standard error.
+
+- A restart argument can be used to rerun a thread target function if
+ there's an uncaught exception. Value passed to the restart argument
+ is passed to time.sleep before restarting the function.
+
There's also a Process constructor/decorator that works like Thread,
but with multi-processing processes, and without the ``value`` and
``exception`` attributes.
Modified: zc.thread/trunk/src/zc/thread/__init__.py
===================================================================
--- zc.thread/trunk/src/zc/thread/__init__.py 2013-01-01 20:41:06 UTC (rev 128983)
+++ zc.thread/trunk/src/zc/thread/__init__.py 2013-01-01 21:08:21 UTC (rev 128984)
@@ -12,22 +12,44 @@
#
##############################################################################
-def _options(daemon=True, start=True, args=(), kwargs=None):
- return daemon, start, args, kwargs or {}
+import atexit
+exiting = False
+
+ at atexit.register
+def _():
+ global exiting
+ exiting = True
+
+def _options(daemon=True, start=True, args=(), kwargs=None, restart=False):
+ return daemon, start, args, kwargs or {}, restart
+
def _Thread(class_, func, options):
- daemon, start, args, kwargs = _options(**options)
+ daemon, start, args, kwargs, restart = _options(**options)
- def run(*args, **kw):
- try:
- v = func(*args, **kw)
- thread.value = v
- except Exception, v:
- thread.exception = v
-
name = "%s.%s" % (getattr(func, '__module__', None),
getattr(func, '__name__', None))
+ def run(*args, **kw):
+ while 1:
+ try:
+ v = func(*args, **kw)
+ thread.value = v
+ return
+ except Exception, v:
+ thread.exception = v
+ if exiting:
+ return
+ import logging
+ logging.getLogger(name).exception(
+ 'Exception in %s', class_.name)
+ import traceback
+ traceback.print_exc()
+ if not restart:
+ return
+ import time
+ time.sleep(restart)
+
thread = class_(target=run, name=name, args=args, kwargs=kwargs)
if hasattr(thread, 'setDaemon'):
More information about the checkins
mailing list