# Path: lib/workerpool/workers.py
# class Worker(Thread):
#     """
#     A loyal worker who will pull jobs from the `jobs` queue and perform them.
# 
#     The run method will get jobs from the `jobs` queue passed into the
#     constructor, and execute them. After each job, task_done() must be executed
#     on the `jobs` queue in order for the pool to know when no more jobs are
#     being processed.
#     """
# 
#     def __init__(self, jobs):
#         self.jobs = jobs
#         Thread.__init__(self)
# 
#     def run(self):
#         "Get jobs from the queue and perform them as they arrive."
#         while 1:
#             # Sleep until there is a job to perform.
#             job = self.jobs.get()
# 
#             # Yawn. Time to get some work done.
#             try:
#                 job.run()
#                 self.jobs.task_done()
#             except TerminationNotice:
#                 self.jobs.task_done()
#                 break 
# 
# Path: lib/workerpool/jobs.py
# class SimpleJob(Job):
#     """
#     Given a `result` queue, a `method` pointer, and an `args` dictionary or
#     list, the method will execute r = method(*args) or r = method(**args), 
#     depending on args' type, and perform result.put(r).
#     """
#     def __init__(self, result, method, args=[]):
#         self.result = result
#         self.method = method
#         self.args = args
# 
#     def run(self):
#         if isinstance(self.args, list) or isinstance(self.args, tuple):
#             r = self.method(*self.args)
#         elif isinstance(self.args, dict):
#             r = self.method(**self.args)
#         self._return(r)
# 
#     def _return(self, r):
#         "Handle return value by appending to the ``self.result`` queue."
#         self.result.put(r)
# 
# class SuicideJob(Job):
#     "A worker receiving this job will commit suicide."
#     def run(self, **kw):
#         raise TerminationNotice()


from queue import Queue
    from .QueueWrapper import Queue
from .workers import Worker
from .jobs import SimpleJob, SuicideJob
import collections
        The default worker factory is called with one argument, which is the
        jobs Queue object that it will read from to acquire jobs. The factory
        will produce a Worker object which will be added to the pool.
    """
    def __init__(self, size=1, maxjobs=0, worker_factory=default_worker_factory):
        if not isinstance(worker_factory, collections.Callable):
            raise TypeError("worker_factory must be callable")

        self.worker_factory = worker_factory # Used to build new workers
        self._size = 0 # Number of active workers we have

        # Initialize the Queue
        Queue.__init__(self, maxjobs) # The queue contains job that are read by workers
        self._jobs = self # Pointer to the queue, for backwards compatibility with version 0.9.1 and earlier

        # Hire some workers!
        for i in range(size):
            self.grow()

    def grow(self):
        "Add another worker to the pool."
        t = self.worker_factory(self)
        t.start()
        self._size += 1

    def shrink(self):
        "Get rid of one worker from the pool. Raises IndexError if empty."
        if self._size <= 0:
            raise IndexError("pool is already empty")
        self._size -= 1

# Path: lib/workerpool/workers.py
# class Worker(Thread):
#     """
#     A loyal worker who will pull jobs from the `jobs` queue and perform them.
# 
#     The run method will get jobs from the `jobs` queue passed into the
#     constructor, and execute them. After each job, task_done() must be executed
#     on the `jobs` queue in order for the pool to know when no more jobs are
#     being processed.
#     """
# 
#     def __init__(self, jobs):
#         self.jobs = jobs
#         Thread.__init__(self)
# 
#     def run(self):
#         "Get jobs from the queue and perform them as they arrive."
#         while 1:
#             # Sleep until there is a job to perform.
#             job = self.jobs.get()
# 
#             # Yawn. Time to get some work done.
#             try:
#                 job.run()
#                 self.jobs.task_done()
#             except TerminationNotice:
#                 self.jobs.task_done()
#                 break 
# 
# Path: lib/workerpool/jobs.py
# class SimpleJob(Job):
#     """
#     Given a `result` queue, a `method` pointer, and an `args` dictionary or
#     list, the method will execute r = method(*args) or r = method(**args), 
#     depending on args' type, and perform result.put(r).
#     """
#     def __init__(self, result, method, args=[]):
#         self.result = result
#         self.method = method
#         self.args = args
# 
#     def run(self):
#         if isinstance(self.args, list) or isinstance(self.args, tuple):
#             r = self.method(*self.args)
#         elif isinstance(self.args, dict):
#             r = self.method(**self.args)
#         self._return(r)
# 
#     def _return(self, r):
#         "Handle return value by appending to the ``self.result`` queue."
#         self.result.put(r)
# 
# class SuicideJob(Job):
#     "A worker receiving this job will commit suicide."
#     def run(self, **kw):
#         raise TerminationNotice()


# Path: lib/workerpool/pools.py
from queue import Queue
    from .QueueWrapper import Queue
from .workers import Worker
from .jobs import SimpleJob, SuicideJob
import collections

        The default worker factory is called with one argument, which is the
        jobs Queue object that it will read from to acquire jobs. The factory
        will produce a Worker object which will be added to the pool.
    """
    def __init__(self, size=1, maxjobs=0, worker_factory=default_worker_factory):
        if not isinstance(worker_factory, collections.Callable):
            raise TypeError("worker_factory must be callable")

        self.worker_factory = worker_factory # Used to build new workers
        self._size = 0 # Number of active workers we have

        # Initialize the Queue
        Queue.__init__(self, maxjobs) # The queue contains job that are read by workers
        self._jobs = self # Pointer to the queue, for backwards compatibility with version 0.9.1 and earlier

        # Hire some workers!
        for i in range(size):
            self.grow()

    def grow(self):
        "Add another worker to the pool."
        t = self.worker_factory(self)
        t.start()
        self._size += 1

    def shrink(self):
        "Get rid of one worker from the pool. Raises IndexError if empty."
        if self._size <= 0:
            raise IndexError("pool is already empty")
        self._size -= 1

        self.put(SuicideJob())