def test_inspect_data_to_log(self): worker_pool = Pool(processes=1, worker_names='WorkerThread') tso = ThreadStateObserver() messages = [] def save_messages(message): messages.append(message) tso.write_to_log = save_messages def sleep(sleep_time, **kwargs): time.sleep(sleep_time) args = (2, ) kwds = {'x': 2} worker_pool.apply_async(func=sleep, args=args, kwds=kwds) # Let the worker get the task time.sleep(0.3) worker_states = worker_pool.inspect_threads() tso.inspect_data_to_log(worker_pool, worker_states) self.assertEqual(len(messages), 2, messages) message_re = ( 'Worker with ID .*? has been running job .*? for .*? seconds.' ' The job is: .*?(.*?, kwargs=.*?)') self.assertRegexpMatches(messages[0], message_re) self.assertEqual(messages[1], '0% of WorkerThread workers are idle.')
def test_inspect_threads(self): worker_pool = Pool(processes=1, worker_names='WorkerThread') def sleep(sleep_time, **kwargs): time.sleep(sleep_time) args = (2, ) kwds = {'x': 2} worker_pool.apply_async(func=sleep, args=args, kwds=kwds) # Let the worker get the task time.sleep(0.3) worker_states = worker_pool.inspect_threads() self.assertEqual(len(worker_states), 1) worker_state = worker_states[0] self.assertEqual(worker_state['func_name'], 'sleep') self.assertEqual(worker_state['args'], args) self.assertEqual(worker_state['kwargs'], kwds) self.assertEqual(worker_state['idle'], False)
def test_inspect_threads(self): worker_pool = Pool(processes=1, worker_names='WorkerThread') def sleep(sleep_time, **kwargs): time.sleep(sleep_time) args = (2,) kwds = {'x': 2} worker_pool.apply_async(func=sleep, args=args, kwds=kwds) # Let the worker get the task time.sleep(0.3) worker_states = worker_pool.inspect_threads() self.assertEqual(len(worker_states), 1) worker_state = worker_states[0] self.assertEqual(worker_state['func_name'], 'sleep') self.assertEqual(worker_state['args'], args) self.assertEqual(worker_state['kwargs'], kwds) self.assertEqual(worker_state['idle'], False)