import logging import operator import os import re import termcolor import timeit from nose.plugins import Plugin # try to import Queue try: import Queue except ImportError: import queue as Queue # define constants IS_NT = os.name == 'nt' # Windows and Python 2.7 multiprocessing don't marry well. _results_queue = None if not IS_NT: import multiprocessing from multiprocessing import queues print(multiprocessing.Queue) class TimerQueue(queues.Queue): """A portable implementation of multiprocessing.Queue. Because of multithreading / multiprocessing semantics, Queue.qsize() may raise the NotImplementedError exception on Unix platforms like Mac OS X where sem_getvalue() is not implemented. This subclass addresses this problem by using a synchronized shared counter (initialized to zero) and increasing / decreasing its value every time the put() and get() methods are called, respectively. This not only prevents NotImplementedError from being raised, but also allows us to implement a reliable version of both qsize() and empty(). """ def __init__(self, *args, **kwargs): if hasattr(multiprocessing, 'get_context'): kwargs.update(ctx=multiprocessing.get_context()) super(TimerQueue, self).__init__(*args, **kwargs) self.size = multiprocessing.Value('i', 0) def put(self, *args, **kwargs): with self.size.get_lock(): self.size.value += 1 super(TimerQueue, self).put(*args, **kwargs) def get(self, *args, **kwargs): with self.size.get_lock(): self.size.value -= 1 return super(TimerQueue, self).get(*args, **kwargs) def qsize(self): """Reliable implementation of multiprocessing.Queue.qsize().""" return self.size.value def empty(self): """Reliable implementation of multiprocessing.Queue.empty().""" return not self.qsize() _results_queue = TimerQueue() log = logging.getLogger('nose.plugin.timer') class TimerPlugin(Plugin): """This plugin provides test timings.""" name = 'timer' score = 1 time_format = re.compile(r'^(?P