Example #1
0
    def __init__ (self, repo, repo_uri, jobs_done = True, poolsize = POOL_SIZE, queuesize = None):
        self.jobs_done = jobs_done

        self.queue = AsyncQueue (queuesize or 0)
        if self.jobs_done:
            self.done = AsyncQueue ()
            
        for i in range (poolsize):
            rep = rh.create_repository (repo.get_type (), repo.get_uri ())
            thread = threading.Thread (target=self._job_thread, args=(rep, repo_uri))
            thread.setDaemon (True)
            thread.start ()
Example #2
0
class JobPool:

    POOL_SIZE = 5

    def __init__ (self, repo, repo_uri, jobs_done = True, poolsize = POOL_SIZE, queuesize = None):
        self.jobs_done = jobs_done

        self.queue = AsyncQueue (queuesize or 0)
        if self.jobs_done:
            self.done = AsyncQueue ()
            
        for i in range (poolsize):
            rep = rh.create_repository (repo.get_type (), repo.get_uri ())
            thread = threading.Thread (target=self._job_thread, args=(rep, repo_uri))
            thread.setDaemon (True)
            thread.start ()

    def _job_thread (self, repo, repo_uri):
        while True:
            job = self.queue.get ()
            job.run (repo, repo_uri)
            self.queue.done ()

            if self.jobs_done:
                self.done.put (job)

    def push (self, job):
        self.queue.put (job)

    def get_next_done (self, timeout = None):
        if not self.jobs_done:
            return None
        
        try:
            job = self.done.get (timeout)
            self.done.done ()
            return job
        except TimeOut:
            return None

    def get_next_done_unlocked (self):
        if not self.jobs_done:
            return None

        if self.done.empty_unlocked ():
            return None
        
        return self.done.get_unlocked ()

    def join (self):
        self.queue.join ()