コード例 #1
0
ファイル: threads_test.py プロジェクト: yanghaier/pygame
    def test_do(self):
        """ Tests function placement on queue and execution after blocking function completion."""

        # __doc__ (as of 2008-06-28) for pygame.threads.WorkerQueue.do:

        # puts a function on a queue for running _later_.

        def sleep_test():
            time.sleep(0.5)

        def calc_test(x):
            return x + 1

        worker_queue = WorkerQueue(num_workers=1)
        sleep_return = FuncResult(sleep_test)
        calc_return = FuncResult(calc_test)
        init_time = time.time()
        worker_queue.do(sleep_return)
        worker_queue.do(calc_return, 1)
        worker_queue.wait()
        worker_queue.stop()
        time_diff = time.time() - init_time

        self.assertEqual(sleep_return.result, None)
        self.assertEqual(calc_return.result, 2)
        self.assertGreaterEqual(time_diff, 0.5)
コード例 #2
0
ファイル: threads_test.py プロジェクト: edummap4412/Estudos
    def test_threadloop(self):

        # __doc__ (as of 2008-06-28) for pygame.threads.WorkerQueue.threadloop:

        # Loops until all of the tasks are finished.

        #Make a worker queue with only one thread
        wq = WorkerQueue(1)

        #Ocuppy the one worker with the threadloop
        #wq threads are just threadloop, so this makes an embedded threadloop
        wq.do(wq.threadloop)

        #Make sure wq can still do work
        #If wq can still do work, threadloop works
        l = []
        wq.do(l.append, 1)
        #Wait won't work because the primary thread is in an infinite loop
        time.sleep(.5)
        self.assertEqual(l[0], 1)

        #Kill the embedded threadloop by sending stop onto the stack
        #Threadloop puts STOP back onto the queue when it STOPs so this kills both loops
        wq.stop()

        #Make sure wq has stopped
        self.assertFalse(wq.pool[0].is_alive())
コード例 #3
0
ファイル: threads_test.py プロジェクト: edummap4412/Estudos
    def test_usage_with_different_functions(self):
        def f(x):
            return x + 1

        def f2(x):
            return x + 2

        wq = WorkerQueue()
        fr = FuncResult(f)
        fr2 = FuncResult(f2)
        wq.do(fr, 1)
        wq.do(fr2, 1)
        wq.wait()
        wq.stop()

        self.assertEqual(fr.result, 2)
        self.assertEqual(fr2.result, 3)
コード例 #4
0
    def test_usage_with_different_functions(self):
        def f(x):
            return x+1
        
        def f2(x):
            return x+2
        
        wq = WorkerQueue()
        fr = FuncResult(f)
        fr2 = FuncResult(f2)
        wq.do(fr, 1)
        wq.do(fr2, 1)
        wq.wait()
        wq.stop()

        self.assert_(fr.result  == 2)
        self.assertEqual(fr2.result, 3)
コード例 #5
0
    def test_wait(self):

        # __doc__ (as of 2008-06-28) for pygame.threads.WorkerQueue.wait:

          # waits until all tasks are complete.

        wq = WorkerQueue()
        
        for i in xrange_(2000): wq.do(lambda x: x+1, i)
        wq.wait()

        self.assertRaises(Empty, wq.queue.get_nowait)

        wq.stop()
コード例 #6
0
ファイル: threads_test.py プロジェクト: arnthor89/pygame
    def test_tmap__no_workers(self):
        '''Ensure that map is called if the number of workers is set to 0'''
        r = range(1000)

        # Calculate the expected result of the computation
        expected = list(map(lambda x: x + 1, r))

        # Ask tmap to do the computation with 0 workers
        r1 = tmap(lambda x: x + 1, r, num_workers=0)

        self.assertEqual(list(r1), expected)

        # Send tmap an empty worker queue and verify that it uses map in this case too
        wq = WorkerQueue(0)
        r2 = tmap(lambda x: x + 1, r, worker_queue=wq)

        self.assertEqual(list(r2), expected)
コード例 #7
0
ファイル: threads_test.py プロジェクト: edummap4412/Estudos
    def test_stop(self):
        """Ensure stop() stops the worker queue"""
        wq = WorkerQueue()

        self.assertGreater(len(wq.pool), 0)

        for t in wq.pool:
            self.assertTrue(t.is_alive())

        for i in xrange_(200):
            wq.do(lambda x: x + 1, i)

        wq.stop()

        for t in wq.pool:
            self.assertFalse(t.is_alive())

        self.assertIs(wq.queue.get(), STOP)
コード例 #8
0
    def test_stop(self):

        # __doc__ (as of 2008-06-28) for pygame.threads.WorkerQueue.stop:

          # Stops the WorkerQueue, waits for all of the threads to finish up.
          #         
        
        wq = WorkerQueue()
        
        self.assert_(len(wq.pool) > 0)
        for t in wq.pool: self.assert_(t.isAlive())
        
        for i in xrange_(200): wq.do(lambda x: x+1, i)
        
        wq.stop()
        for t in wq.pool: self.assert_(not t.isAlive())
        
        self.assert_(wq.queue.get() is STOP)