Ejemplo n.º 1
0
    def test_speed(self):
        # Initialize manually because we change a fundemental setting
        initialize(force=True)

        # Sync
        t_start = time.time()
        s_sync = ""
        for user in users:
            s_sync += expensive_render(user)
        duration_sync = time.time() - t_start

        # Async. Break into chunks of tasks.
        t_start = time.time()
        task = Task()
        for start, end in ranges(users):
            task.run(multi_expensive_render, start, end)

        s_async = "".join(task.get())
        duration_async = time.time() - t_start

        # Hopefully we're on a multicore machine :)
        self.assertEqual(s_sync, s_async)
        self.failUnless(duration_async < duration_sync)
Ejemplo n.º 2
0
        print(f'{i},')


def expensive_as_string(item):
    time.sleep(0.01)
    return str(item)


def multi_expensive_as_string(start, end):
    return ",".join([expensive_as_string(item) for item in items[start:end]])


if __name__ == "__main__":

    # Needs to be called only once for lifetime of process
    initialize()

    # Example 1: trivial (and slightly pointless) usage
    task = Task()

    task.run(as_string, 2000)

    # Example 2: divide job optimally using ranges function
    #task = Task()
    #for start, end in ranges(items):
    # Note we don't pass items because pickling is expensive and defeats
    # the purpose of the exercize.
    #   task.run(multi_expensive_as_string, start, end)
    # print(", ".join(task.get()))

    # Stop the multicore workers
Ejemplo n.º 3
0
 def ready(self):
     initialize()
Ejemplo n.º 4
0
 def test_no_deadlock(self):
     initialize(force=True)
Ejemplo n.º 5
0
 def setUpClass(cls):
     super(TaskTestCase, cls).setUpClass()
     initialize()