示例#1
0
 def test_priority(self):
     threads = []
     t1 = time.time_ns()
     threads.append(create_thread(self.f3))
     threads.append(create_thread(self.f2))
     threads.append(create_thread(self.f1))
     concurrent.futures.wait(threads)
     t2 = time.time_ns()
     delta = (t2 - t1) * (10**-9)
     self.assertGreaterEqual(delta, 5)
     self.assertLessEqual(delta, 5.5)
     self.assertEqual((1, 2, 3), tuple(results))
示例#2
0
def create_process(func: Callable, *args: Any, **kwargs: Any) -> Future:
    '''
    Calls a function in its own process
    :param func: The function to be called
    :param args: The function arguments
    :param kwargs: The function keyword arguments
    :return: The created Future object, from which we can call 'result()' to get the function return value.
    '''
    tp = ProcessPoolExecutor(max_workers=1)
    future = tp.submit(func, *args, **kwargs)
    create_thread(tp.shutdown, wait=True)  # Necessary since wait=False is broken
    return future
示例#3
0
 def test_m2_three_seconds_three_processes(self):
     threads = []
     t1 = time.time_ns()
     for _ in range(3):
         threads.append(create_thread(self.m2_three_seconds_three_threads))
     concurrent.futures.wait(threads)
     t2 = time.time_ns()
     delta = (t2 - t1) * (10**-9)
     self.assertGreaterEqual(delta, 3)
     self.assertLessEqual(delta, 3.5)